linux-user: stack_base is now mandatory on all targets
[qemu.git] / hw / xen_devconfig.c
1 #include "xen_backend.h"
2 #include "blockdev.h"
3 #include "block_int.h" /* XXX */
4
5 /* ------------------------------------------------------------- */
6
7 struct xs_dirs {
8 char *xs_dir;
9 QTAILQ_ENTRY(xs_dirs) list;
10 };
11 static QTAILQ_HEAD(xs_dirs_head, xs_dirs) xs_cleanup = QTAILQ_HEAD_INITIALIZER(xs_cleanup);
12
13 static void xen_config_cleanup_dir(char *dir)
14 {
15 struct xs_dirs *d;
16
17 d = g_malloc(sizeof(*d));
18 d->xs_dir = dir;
19 QTAILQ_INSERT_TAIL(&xs_cleanup, d, list);
20 }
21
22 void xen_config_cleanup(void)
23 {
24 struct xs_dirs *d;
25
26 QTAILQ_FOREACH(d, &xs_cleanup, list) {
27 xs_rm(xenstore, 0, d->xs_dir);
28 }
29 }
30
31 /* ------------------------------------------------------------- */
32
33 static int xen_config_dev_mkdir(char *dev, int p)
34 {
35 struct xs_permissions perms[2] = {{
36 .id = 0, /* set owner: dom0 */
37 },{
38 .id = xen_domid,
39 .perms = p,
40 }};
41
42 if (!xs_mkdir(xenstore, 0, dev)) {
43 xen_be_printf(NULL, 0, "xs_mkdir %s: failed\n", dev);
44 return -1;
45 }
46 xen_config_cleanup_dir(g_strdup(dev));
47
48 if (!xs_set_permissions(xenstore, 0, dev, perms, 2)) {
49 xen_be_printf(NULL, 0, "xs_set_permissions %s: failed\n", dev);
50 return -1;
51 }
52 return 0;
53 }
54
55 static int xen_config_dev_dirs(const char *ftype, const char *btype, int vdev,
56 char *fe, char *be, int len)
57 {
58 char *dom;
59
60 dom = xs_get_domain_path(xenstore, xen_domid);
61 snprintf(fe, len, "%s/device/%s/%d", dom, ftype, vdev);
62 free(dom);
63
64 dom = xs_get_domain_path(xenstore, 0);
65 snprintf(be, len, "%s/backend/%s/%d/%d", dom, btype, xen_domid, vdev);
66 free(dom);
67
68 xen_config_dev_mkdir(fe, XS_PERM_READ | XS_PERM_WRITE);
69 xen_config_dev_mkdir(be, XS_PERM_READ);
70 return 0;
71 }
72
73 static int xen_config_dev_all(char *fe, char *be)
74 {
75 /* frontend */
76 if (xen_protocol)
77 xenstore_write_str(fe, "protocol", xen_protocol);
78
79 xenstore_write_int(fe, "state", XenbusStateInitialising);
80 xenstore_write_int(fe, "backend-id", 0);
81 xenstore_write_str(fe, "backend", be);
82
83 /* backend */
84 xenstore_write_str(be, "domain", qemu_name ? qemu_name : "no-name");
85 xenstore_write_int(be, "online", 1);
86 xenstore_write_int(be, "state", XenbusStateInitialising);
87 xenstore_write_int(be, "frontend-id", xen_domid);
88 xenstore_write_str(be, "frontend", fe);
89
90 return 0;
91 }
92
93 /* ------------------------------------------------------------- */
94
95 int xen_config_dev_blk(DriveInfo *disk)
96 {
97 char fe[256], be[256];
98 int vdev = 202 * 256 + 16 * disk->unit;
99 int cdrom = disk->media_cd;
100 const char *devtype = cdrom ? "cdrom" : "disk";
101 const char *mode = cdrom ? "r" : "w";
102
103 snprintf(disk->bdrv->device_name, sizeof(disk->bdrv->device_name),
104 "xvd%c", 'a' + disk->unit);
105 xen_be_printf(NULL, 1, "config disk %d [%s]: %s\n",
106 disk->unit, disk->bdrv->device_name, disk->bdrv->filename);
107 xen_config_dev_dirs("vbd", "qdisk", vdev, fe, be, sizeof(fe));
108
109 /* frontend */
110 xenstore_write_int(fe, "virtual-device", vdev);
111 xenstore_write_str(fe, "device-type", devtype);
112
113 /* backend */
114 xenstore_write_str(be, "dev", disk->bdrv->device_name);
115 xenstore_write_str(be, "type", "file");
116 xenstore_write_str(be, "params", disk->bdrv->filename);
117 xenstore_write_str(be, "mode", mode);
118
119 /* common stuff */
120 return xen_config_dev_all(fe, be);
121 }
122
123 int xen_config_dev_nic(NICInfo *nic)
124 {
125 char fe[256], be[256];
126 char mac[20];
127
128 snprintf(mac, sizeof(mac), "%02x:%02x:%02x:%02x:%02x:%02x",
129 nic->macaddr.a[0], nic->macaddr.a[1], nic->macaddr.a[2],
130 nic->macaddr.a[3], nic->macaddr.a[4], nic->macaddr.a[5]);
131 xen_be_printf(NULL, 1, "config nic %d: mac=\"%s\"\n", nic->vlan->id, mac);
132 xen_config_dev_dirs("vif", "qnic", nic->vlan->id, fe, be, sizeof(fe));
133
134 /* frontend */
135 xenstore_write_int(fe, "handle", nic->vlan->id);
136 xenstore_write_str(fe, "mac", mac);
137
138 /* backend */
139 xenstore_write_int(be, "handle", nic->vlan->id);
140 xenstore_write_str(be, "mac", mac);
141
142 /* common stuff */
143 return xen_config_dev_all(fe, be);
144 }
145
146 int xen_config_dev_vfb(int vdev, const char *type)
147 {
148 char fe[256], be[256];
149
150 xen_config_dev_dirs("vfb", "vfb", vdev, fe, be, sizeof(fe));
151
152 /* backend */
153 xenstore_write_str(be, "type", type);
154
155 /* common stuff */
156 return xen_config_dev_all(fe, be);
157 }
158
159 int xen_config_dev_vkbd(int vdev)
160 {
161 char fe[256], be[256];
162
163 xen_config_dev_dirs("vkbd", "vkbd", vdev, fe, be, sizeof(fe));
164 return xen_config_dev_all(fe, be);
165 }
166
167 int xen_config_dev_console(int vdev)
168 {
169 char fe[256], be[256];
170
171 xen_config_dev_dirs("console", "console", vdev, fe, be, sizeof(fe));
172 return xen_config_dev_all(fe, be);
173 }