summaryrefslogtreecommitdiffstats
path: root/sys/xen/xenbus/xenbus_xs.c
blob: 93122553e0cff1fbdb9a4eee4a94fdaf7331e944 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
/******************************************************************************
 * xenbus_xs.c
 *
 * This is the kernel equivalent of the "xs" library.  We don't need everything
 * and we use xenbus_comms for communication.
 *
 * Copyright (C) 2005 Rusty Russell, IBM Corporation
 * 
 * This file may be distributed separately from the Linux kernel, or
 * incorporated into other software packages, subject to the following license:
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this source file (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy, modify,
 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
 * and to permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */


#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/uio.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/sx.h>
#include <sys/syslog.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <sys/proc.h>
#include <sys/kthread.h>
#include <sys/unistd.h>

#include <machine/xen/xen-os.h>
#include <xen/hypervisor.h>
#include <machine/stdarg.h>

#include <xen/xenbus/xenbusvar.h>
#include <xen/xenbus/xenbus_comms.h>
#include <xen/interface/hvm/params.h>

#include <vm/vm.h>
#include <vm/pmap.h>

static int xs_process_msg(enum xsd_sockmsg_type *type);

int xenwatch_running = 0;
int xenbus_running = 0;
int xen_store_evtchn;

struct xs_stored_msg {
	TAILQ_ENTRY(xs_stored_msg) list;

	struct xsd_sockmsg hdr;

	union {
		/* Queued replies. */
		struct {
			char *body;
		} reply;

		/* Queued watch events. */
		struct {
			struct xenbus_watch *handle;
			char **vec;
			unsigned int vec_size;
		} watch;
	} u;
};

struct xs_handle {
	/* A list of replies. Currently only one will ever be outstanding. */
	TAILQ_HEAD(xs_handle_list, xs_stored_msg) reply_list;
	struct mtx reply_lock;
	int reply_waitq;

	/* One request at a time. */
	struct sx request_mutex;

	/* Protect transactions against save/restore. */
	struct sx suspend_mutex;
};

static struct xs_handle xs_state;

/* List of registered watches, and a lock to protect it. */
static LIST_HEAD(watch_list_head, xenbus_watch) watches;
static struct mtx watches_lock;
/* List of pending watch callback events, and a lock to protect it. */
static TAILQ_HEAD(event_list_head, xs_stored_msg) watch_events;
static struct mtx watch_events_lock;

/*
 * Details of the xenwatch callback kernel thread. The thread waits on the
 * watch_events_waitq for work to do (queued on watch_events list). When it
 * wakes up it acquires the xenwatch_mutex before reading the list and
 * carrying out work.
 */
static pid_t xenwatch_pid;
struct sx xenwatch_mutex;
static int watch_events_waitq;

#define xsd_error_count	(sizeof(xsd_errors) / sizeof(xsd_errors[0]))

static int
xs_get_error(const char *errorstring)
{
	unsigned int i;

	for (i = 0; i < xsd_error_count; i++) {
		if (!strcmp(errorstring, xsd_errors[i].errstring))
			return (xsd_errors[i].errnum);
	}
	log(LOG_WARNING, "XENBUS xen store gave: unknown error %s",
	    errorstring);
	return (EINVAL);
}

extern void kdb_backtrace(void);

static int
xs_read_reply(enum xsd_sockmsg_type *type, unsigned int *len, void **result)
{
	struct xs_stored_msg *msg;
	char *body;
	int error;

	mtx_lock(&xs_state.reply_lock);

	while (TAILQ_EMPTY(&xs_state.reply_list)) {
		while (TAILQ_EMPTY(&xs_state.reply_list)) {
			error = mtx_sleep(&xs_state.reply_waitq,
			    &xs_state.reply_lock,
			    PCATCH, "xswait", hz/10);
			if (error && error != EWOULDBLOCK) {
				mtx_unlock(&xs_state.reply_lock);
				return (error);
			}
		}
	}

	msg = TAILQ_FIRST(&xs_state.reply_list);
	TAILQ_REMOVE(&xs_state.reply_list, msg, list);

	mtx_unlock(&xs_state.reply_lock);

	*type = msg->hdr.type;
	if (len)
		*len = msg->hdr.len;
	body = msg->u.reply.body;

	free(msg, M_DEVBUF);
	*result = body;
	return (0);
}

#if 0
/* Emergency write. UNUSED*/
void xenbus_debug_write(const char *str, unsigned int count)
{
	struct xsd_sockmsg msg = { 0 };

	msg.type = XS_DEBUG;
	msg.len = sizeof("print") + count + 1;

	sx_xlock(&xs_state.request_mutex);
	xb_write(&msg, sizeof(msg));
	xb_write("print", sizeof("print"));
	xb_write(str, count);
	xb_write("", 1);
	sx_xunlock(&xs_state.request_mutex);
}

#endif

int
xenbus_dev_request_and_reply(struct xsd_sockmsg *msg, void **result)
{
	struct xsd_sockmsg req_msg = *msg;
	int error;

	if (req_msg.type == XS_TRANSACTION_START)
		sx_slock(&xs_state.suspend_mutex);

	sx_xlock(&xs_state.request_mutex);

	error = xb_write(msg, sizeof(*msg) + msg->len,
	    &xs_state.request_mutex.lock_object);
	if (error) {
		msg->type = XS_ERROR;
	} else {
		error = xs_read_reply(&msg->type, &msg->len, result);
	}

	sx_xunlock(&xs_state.request_mutex);

	if ((msg->type == XS_TRANSACTION_END) ||
	    ((req_msg.type == XS_TRANSACTION_START) &&
		(msg->type == XS_ERROR)))
		sx_sunlock(&xs_state.suspend_mutex);

	return (error);
}

/*
 * Send message to xs. The reply is returned in *result and should be
 * fred with free(*result, M_DEVBUF). Return zero on success or an
 * error code on failure.
 */
static int
xs_talkv(struct xenbus_transaction t, enum xsd_sockmsg_type type,
    const struct iovec *iovec, unsigned int num_vecs,
    unsigned int *len, void **result)
{
	struct xsd_sockmsg msg;
	void *ret = NULL;
	unsigned int i;
	int error;

	msg.tx_id = t.id;
	msg.req_id = 0;
	msg.type = type;
	msg.len = 0;
	for (i = 0; i < num_vecs; i++)
		msg.len += iovec[i].iov_len;

	sx_xlock(&xs_state.request_mutex);

	error = xb_write(&msg, sizeof(msg),
	    &xs_state.request_mutex.lock_object);
	if (error) {
		sx_xunlock(&xs_state.request_mutex);
		printf("xs_talkv failed %d\n", error);
		return (error);
	}

	for (i = 0; i < num_vecs; i++) {
		error = xb_write(iovec[i].iov_base, iovec[i].iov_len,
		    &xs_state.request_mutex.lock_object);
		if (error) {		
			sx_xunlock(&xs_state.request_mutex);
			printf("xs_talkv failed %d\n", error);
			return (error);
		}
	}

	error = xs_read_reply(&msg.type, len, &ret);

	sx_xunlock(&xs_state.request_mutex);

	if (error)
		return (error);

	if (msg.type == XS_ERROR) {
		error = xs_get_error(ret);
		free(ret, M_DEVBUF);
		return (error);
	}

#if 0
	if ((xenwatch_running == 0) && (xenwatch_inline == 0)) {
		xenwatch_inline = 1;
		while (!TAILQ_EMPTY(&watch_events) 
		    && xenwatch_running == 0) {
						
			struct xs_stored_msg *wmsg = TAILQ_FIRST(&watch_events);
			TAILQ_REMOVE(&watch_events, wmsg, list);
						
			wmsg->u.watch.handle->callback(
				wmsg->u.watch.handle,
				(const char **)wmsg->u.watch.vec,
				wmsg->u.watch.vec_size);
			free(wmsg->u.watch.vec, M_DEVBUF);
			free(wmsg, M_DEVBUF);
		}
		xenwatch_inline = 0;
	}
#endif
	KASSERT(msg.type == type, ("bad xenstore message type"));

	if (result)
		*result = ret;
	else
		free(ret, M_DEVBUF);

	return (0);
}

/* Simplified version of xs_talkv: single message. */
static int
xs_single(struct xenbus_transaction t, enum xsd_sockmsg_type type,
    const char *string, unsigned int *len, void **result)
{
	struct iovec iovec;

	iovec.iov_base = (void *)(uintptr_t) string;
	iovec.iov_len = strlen(string) + 1;

	return (xs_talkv(t, type, &iovec, 1, len, result));
}

static unsigned int
count_strings(const char *strings, unsigned int len)
{
	unsigned int num;
	const char *p;

	for (p = strings, num = 0; p < strings + len; p += strlen(p) + 1)
		num++;

	return num;
}

/* Return the path to dir with /name appended. Buffer must be kfree()'ed. */ 
static char *
join(const char *dir, const char *name)
{
	char *buffer;

	buffer = malloc(strlen(dir) + strlen("/") + strlen(name) + 1,
	    M_DEVBUF, M_WAITOK);

	strcpy(buffer, dir);
	if (strcmp(name, "")) {
		strcat(buffer, "/");
		strcat(buffer, name);
	}

	return (buffer);
}

static char **
split(char *strings, unsigned int len, unsigned int *num)
{
	char *p, **ret;

	/* Count the strings. */
	*num = count_strings(strings, len) + 1;

	/* Transfer to one big alloc for easy freeing. */
	ret = malloc(*num * sizeof(char *) + len, M_DEVBUF, M_WAITOK);
	memcpy(&ret[*num], strings, len);
	free(strings, M_DEVBUF);

	strings = (char *)&ret[*num];
	for (p = strings, *num = 0; p < strings + len; p += strlen(p) + 1)
		ret[(*num)++] = p;

	ret[*num] = strings + len;
		
	return ret;
}

/*
 * Return the contents of a directory in *result which should be freed
 * with free(*result, M_DEVBUF).
 */
int
xenbus_directory(struct xenbus_transaction t, const char *dir,
    const char *node, unsigned int *num, char ***result)
{
	char *strings, *path;
	unsigned int len = 0;
	int error;

	path = join(dir, node);
	error = xs_single(t, XS_DIRECTORY, path, &len, (void **) &strings);
	free(path, M_DEVBUF);
	if (error)
		return (error);

	*result = split(strings, len, num);
	return (0);
}

/*
 * Check if a path exists. Return 1 if it does.
 */
int
xenbus_exists(struct xenbus_transaction t, const char *dir, const char *node)
{
	char **d;
	int error, dir_n;

	error = xenbus_directory(t, dir, node, &dir_n, &d);
	if (error)
		return (0);
	free(d, M_DEVBUF);
	return (1);
}

/*
 * Get the value of a single file.  Returns the contents in *result
 * which should be freed with free(*result, M_DEVBUF) after use.
 * The length of the value in bytes is returned in *len.
 */
int
xenbus_read(struct xenbus_transaction t, const char *dir, const char *node,
    unsigned int *len, void **result)
{
	char *path;
	void *ret;
	int error;

	path = join(dir, node);
	error = xs_single(t, XS_READ, path, len, &ret);
	free(path, M_DEVBUF);
	if (error)
		return (error);
	*result = ret;
	return (0);
}

/*
 * Write the value of a single file.  Returns error on failure.
 */
int
xenbus_write(struct xenbus_transaction t, const char *dir, const char *node,
    const char *string)
{
	char *path;
	struct iovec iovec[2];
	int error;

	path = join(dir, node);

	iovec[0].iov_base = (void *)(uintptr_t) path;
	iovec[0].iov_len = strlen(path) + 1;
	iovec[1].iov_base = (void *)(uintptr_t) string;
	iovec[1].iov_len = strlen(string);

	error = xs_talkv(t, XS_WRITE, iovec, 2, NULL, NULL);
	free(path, M_DEVBUF);

	return (error);
}

/*
 * Create a new directory.
 */
int
xenbus_mkdir(struct xenbus_transaction t, const char *dir, const char *node)
{
	char *path;
	int ret;

	path = join(dir, node);
	ret = xs_single(t, XS_MKDIR, path, NULL, NULL);
	free(path, M_DEVBUF);

	return (ret);
}

/*
 * Destroy a file or directory (directories must be empty).
 */
int
xenbus_rm(struct xenbus_transaction t, const char *dir, const char *node)
{
	char *path;
	int ret;

	path = join(dir, node);
	ret = xs_single(t, XS_RM, path, NULL, NULL);
	free(path, M_DEVBUF);

	return (ret);
}

/*
 * Start a transaction: changes by others will not be seen during this
 * transaction, and changes will not be visible to others until end.
 */
int
xenbus_transaction_start(struct xenbus_transaction *t)
{
	char *id_str;
	int error;

	sx_slock(&xs_state.suspend_mutex);
	error = xs_single(XBT_NIL, XS_TRANSACTION_START, "", NULL,
	    (void **) &id_str);
	if (error) {
		sx_sunlock(&xs_state.suspend_mutex);
		return (error);
	}

	t->id = strtoul(id_str, NULL, 0);
	free(id_str, M_DEVBUF);

	return (0);
}

/*
 * End a transaction.  If abandon is true, transaction is discarded
 * instead of committed.
 */
int xenbus_transaction_end(struct xenbus_transaction t, int abort)
{
	char abortstr[2];
	int error;

	if (abort)
		strcpy(abortstr, "F");
	else
		strcpy(abortstr, "T");

	error = xs_single(t, XS_TRANSACTION_END, abortstr, NULL, NULL);
		
	sx_sunlock(&xs_state.suspend_mutex);

	return (error);
}

/* Single read and scanf: returns zero or errno. */
int
xenbus_scanf(struct xenbus_transaction t,
    const char *dir, const char *node, int *scancountp, const char *fmt, ...)
{
	va_list ap;
	int error, ns;
	char *val;

	error = xenbus_read(t, dir, node, NULL, (void **) &val);
	if (error)
		return (error);

	va_start(ap, fmt);
	ns = vsscanf(val, fmt, ap);
	va_end(ap);
	free(val, M_DEVBUF);
	/* Distinctive errno. */
	if (ns == 0)
		return (ERANGE);
	if (scancountp)
		*scancountp = ns;
	return (0);
}

/* Single printf and write: returns zero or errno. */
int
xenbus_printf(struct xenbus_transaction t,
    const char *dir, const char *node, const char *fmt, ...)
{
	va_list ap;
	int error, ret;
#define PRINTF_BUFFER_SIZE 4096
	char *printf_buffer;

	printf_buffer = malloc(PRINTF_BUFFER_SIZE, M_DEVBUF, M_WAITOK);

	va_start(ap, fmt);
	ret = vsnprintf(printf_buffer, PRINTF_BUFFER_SIZE, fmt, ap);
	va_end(ap);

	KASSERT(ret <= PRINTF_BUFFER_SIZE-1, ("xenbus_printf: message too large"));
	error = xenbus_write(t, dir, node, printf_buffer);

	free(printf_buffer, M_DEVBUF);

	return (error);
}

/* Takes tuples of names, scanf-style args, and void **, NULL terminated. */
int
xenbus_gather(struct xenbus_transaction t, const char *dir, ...)
{
	va_list ap;
	const char *name;
	int error, i;

	for (i = 0; i < 10000; i++)
		HYPERVISOR_yield();
		
	va_start(ap, dir);
	error = 0;
	while (error == 0 && (name = va_arg(ap, char *)) != NULL) {
		const char *fmt = va_arg(ap, char *);
		void *result = va_arg(ap, void *);
		char *p;

		error = xenbus_read(t, dir, name, NULL, (void **) &p);
		if (error)
			break;

		if (fmt) {
			if (sscanf(p, fmt, result) == 0)
				error = EINVAL;
			free(p, M_DEVBUF);
		} else
			*(char **)result = p;
	}
	va_end(ap);

	return (error);
}

static int
xs_watch(const char *path, const char *token)
{
	struct iovec iov[2];

	iov[0].iov_base = (void *)(uintptr_t) path;
	iov[0].iov_len = strlen(path) + 1;
	iov[1].iov_base = (void *)(uintptr_t) token;
	iov[1].iov_len = strlen(token) + 1;

	return (xs_talkv(XBT_NIL, XS_WATCH, iov, 2, NULL, NULL));
}

static int
xs_unwatch(const char *path, const char *token)
{
	struct iovec iov[2];

	iov[0].iov_base = (void *)(uintptr_t) path;
	iov[0].iov_len = strlen(path) + 1;
	iov[1].iov_base = (void *)(uintptr_t) token;
	iov[1].iov_len = strlen(token) + 1;

	return (xs_talkv(XBT_NIL, XS_UNWATCH, iov, 2, NULL, NULL));
}

static struct xenbus_watch *
find_watch(const char *token)
{
	struct xenbus_watch *i, *cmp;

	cmp = (void *)strtoul(token, NULL, 16);

	LIST_FOREACH(i, &watches, list)
		if (i == cmp)
			return (i);

	return (NULL);
}

/* Register callback to watch this node. */
int
register_xenbus_watch(struct xenbus_watch *watch)
{
	/* Pointer in ascii is the token. */
	char token[sizeof(watch) * 2 + 1];
	int error;

	sprintf(token, "%lX", (long)watch);

	sx_slock(&xs_state.suspend_mutex);

	mtx_lock(&watches_lock);
	KASSERT(find_watch(token) == NULL, ("watch already registered"));
	LIST_INSERT_HEAD(&watches, watch, list);
	mtx_unlock(&watches_lock);

	error = xs_watch(watch->node, token);
		
	/* Ignore errors due to multiple registration. */
	if (error == EEXIST) {
		mtx_lock(&watches_lock);
		LIST_REMOVE(watch, list);
		mtx_unlock(&watches_lock);
	}

	sx_sunlock(&xs_state.suspend_mutex);

	return (error);
}

void
unregister_xenbus_watch(struct xenbus_watch *watch)
{
	struct xs_stored_msg *msg, *tmp;
	char token[sizeof(watch) * 2 + 1];
	int error;

	sprintf(token, "%lX", (long)watch);
		
	sx_slock(&xs_state.suspend_mutex);

	mtx_lock(&watches_lock);
	KASSERT(find_watch(token), ("watch not registered"));
	LIST_REMOVE(watch, list);
	mtx_unlock(&watches_lock);

	error = xs_unwatch(watch->node, token);
	if (error)
		log(LOG_WARNING, "XENBUS Failed to release watch %s: %i\n",
		    watch->node, error);

	sx_sunlock(&xs_state.suspend_mutex);

	/* Cancel pending watch events. */
	mtx_lock(&watch_events_lock);
	TAILQ_FOREACH_SAFE(msg, &watch_events, list, tmp) {
		if (msg->u.watch.handle != watch)
			continue;
		TAILQ_REMOVE(&watch_events, msg, list);
		free(msg->u.watch.vec, M_DEVBUF);
		free(msg, M_DEVBUF);
	}
	mtx_unlock(&watch_events_lock);

	/* Flush any currently-executing callback, unless we are it. :-) */
	if (curproc->p_pid != xenwatch_pid) {
		sx_xlock(&xenwatch_mutex);
		sx_xunlock(&xenwatch_mutex);
	}
}

void
xs_suspend(void)
{	

	sx_xlock(&xs_state.suspend_mutex);
	sx_xlock(&xs_state.request_mutex);
}

void
xs_resume(void)
{
	struct xenbus_watch *watch;
	char token[sizeof(watch) * 2 + 1];

	sx_xunlock(&xs_state.request_mutex);

	/* No need for watches_lock: the suspend_mutex is sufficient. */
	LIST_FOREACH(watch, &watches, list) {
		sprintf(token, "%lX", (long)watch);
		xs_watch(watch->node, token);
	}

	sx_xunlock(&xs_state.suspend_mutex);
}

static void
xenwatch_thread(void *unused)
{
	struct xs_stored_msg *msg;

	for (;;) {

		mtx_lock(&watch_events_lock);
		while (TAILQ_EMPTY(&watch_events))
			mtx_sleep(&watch_events_waitq,
			    &watch_events_lock,
			    PWAIT | PCATCH, "waitev", hz/10);

		mtx_unlock(&watch_events_lock);
		sx_xlock(&xenwatch_mutex);

		mtx_lock(&watch_events_lock);
		msg = TAILQ_FIRST(&watch_events);
		if (msg)
			TAILQ_REMOVE(&watch_events, msg, list);
		mtx_unlock(&watch_events_lock);

		if (msg != NULL) {
			/*
			 * XXX There are messages coming in with a NULL callback.
			 * XXX This deserves further investigation; the workaround
			 * XXX here simply prevents the kernel from panic'ing
			 * XXX on startup.
			 */
			if (msg->u.watch.handle->callback != NULL)
				msg->u.watch.handle->callback(
					msg->u.watch.handle,
					(const char **)msg->u.watch.vec,
					msg->u.watch.vec_size);
			free(msg->u.watch.vec, M_DEVBUF);
			free(msg, M_DEVBUF);
		}

		sx_xunlock(&xenwatch_mutex);
	}
}

static int
xs_process_msg(enum xsd_sockmsg_type *type)
{
	struct xs_stored_msg *msg;
	char *body;
	int error;
		
	msg = malloc(sizeof(*msg), M_DEVBUF, M_WAITOK);
	mtx_lock(&xs_state.reply_lock);
	error = xb_read(&msg->hdr, sizeof(msg->hdr),
	    &xs_state.reply_lock.lock_object);
	mtx_unlock(&xs_state.reply_lock);
	if (error) {
		free(msg, M_DEVBUF);
		return (error);
	}

	body = malloc(msg->hdr.len + 1, M_DEVBUF, M_WAITOK);
	mtx_lock(&xs_state.reply_lock);
	error = xb_read(body, msg->hdr.len,
	    &xs_state.reply_lock.lock_object); 
	mtx_unlock(&xs_state.reply_lock);
	if (error) {
		free(body, M_DEVBUF);
		free(msg, M_DEVBUF);
		return (error);
	}
	body[msg->hdr.len] = '\0';

	*type = msg->hdr.type;
	if (msg->hdr.type == XS_WATCH_EVENT) {
		msg->u.watch.vec = split(body, msg->hdr.len,
		    &msg->u.watch.vec_size);
				
		mtx_lock(&watches_lock);
		msg->u.watch.handle = find_watch(
			msg->u.watch.vec[XS_WATCH_TOKEN]);
		if (msg->u.watch.handle != NULL) {
			mtx_lock(&watch_events_lock);
			TAILQ_INSERT_TAIL(&watch_events, msg, list);
			wakeup(&watch_events_waitq);
			mtx_unlock(&watch_events_lock);
		} else {
			free(msg->u.watch.vec, M_DEVBUF);
			free(msg, M_DEVBUF);
		}
		mtx_unlock(&watches_lock);
	} else {
		msg->u.reply.body = body;
		mtx_lock(&xs_state.reply_lock);
		TAILQ_INSERT_TAIL(&xs_state.reply_list, msg, list);
		wakeup(&xs_state.reply_waitq);
		mtx_unlock(&xs_state.reply_lock);
	}
		
	return 0;
}

static void
xenbus_thread(void *unused)
{
	int error;
	enum xsd_sockmsg_type type;
	xenbus_running = 1;

	for (;;) {
		error = xs_process_msg(&type);
		if (error) 
			printf("XENBUS error %d while reading message\n",
			    error);
	}
}

#ifdef XENHVM
static unsigned long xen_store_mfn;
char *xen_store;

static inline unsigned long
hvm_get_parameter(int index)
{
	struct xen_hvm_param xhv;
	int error;
	
	xhv.domid = DOMID_SELF;
	xhv.index = index;
	error = HYPERVISOR_hvm_op(HVMOP_get_param, &xhv);
	if (error) {
		printf("hvm_get_parameter: failed to get %d, error %d\n",
		    index, error);
		return (0);
	}
	return (xhv.value);
}

#endif

int
xs_init(void)
{
	int error;
	struct proc *p;

#ifdef XENHVM
	xen_store_evtchn = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN);
	xen_store_mfn = hvm_get_parameter(HVM_PARAM_STORE_PFN);
	xen_store = pmap_mapdev(xen_store_mfn * PAGE_SIZE, PAGE_SIZE);
#else
	xen_store_evtchn = xen_start_info->store_evtchn;
#endif

	TAILQ_INIT(&xs_state.reply_list);
	TAILQ_INIT(&watch_events);
	sx_init(&xenwatch_mutex, "xenwatch");

		
	mtx_init(&xs_state.reply_lock, "state reply", NULL, MTX_DEF);
	sx_init(&xs_state.request_mutex, "xenstore request");
	sx_init(&xs_state.suspend_mutex, "xenstore suspend");

		
#if 0
	mtx_init(&xs_state.suspend_mutex, "xenstore suspend", NULL, MTX_DEF);
	sema_init(&xs_state.request_mutex, 1, "xenstore request");
	sema_init(&xenwatch_mutex, 1, "xenwatch");
#endif
	mtx_init(&watches_lock, "watches", NULL, MTX_DEF);
	mtx_init(&watch_events_lock, "watch events", NULL, MTX_DEF);
   
	/* Initialize the shared memory rings to talk to xenstored */
	error = xb_init_comms();
	if (error)
		return (error);

	xenwatch_running = 1;
	error = kproc_create(xenwatch_thread, NULL, &p,
	    RFHIGHPID, 0, "xenwatch");
	if (error)
		return (error);
	xenwatch_pid = p->p_pid;

	error = kproc_create(xenbus_thread, NULL, NULL, 
	    RFHIGHPID, 0, "xenbus");
	
	return (error);
}
OpenPOWER on IntegriCloud