]> Git Repo - qemu.git/blob - hw/s390-virtio.c
qemu-nbd: fix socket creation race
[qemu.git] / hw / s390-virtio.c
1 /*
2  * QEMU S390 virtio target
3  *
4  * Copyright (c) 2009 Alexander Graf <[email protected]>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "hw.h"
21 #include "block.h"
22 #include "blockdev.h"
23 #include "sysemu.h"
24 #include "net.h"
25 #include "boards.h"
26 #include "monitor.h"
27 #include "loader.h"
28 #include "elf.h"
29 #include "hw/virtio.h"
30 #include "hw/sysbus.h"
31 #include "kvm.h"
32 #include "exec-memory.h"
33
34 #include "hw/s390-virtio-bus.h"
35
36 //#define DEBUG_S390
37
38 #ifdef DEBUG_S390
39 #define dprintf(fmt, ...) \
40     do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
41 #else
42 #define dprintf(fmt, ...) \
43     do { } while (0)
44 #endif
45
46 #define KVM_S390_VIRTIO_NOTIFY          0
47 #define KVM_S390_VIRTIO_RESET           1
48 #define KVM_S390_VIRTIO_SET_STATUS      2
49
50 #define KERN_IMAGE_START                0x010000UL
51 #define KERN_PARM_AREA                  0x010480UL
52 #define INITRD_START                    0x800000UL
53 #define INITRD_PARM_START               0x010408UL
54 #define INITRD_PARM_SIZE                0x010410UL
55 #define PARMFILE_START                  0x001000UL
56
57 #define ZIPL_START                      0x009000UL
58 #define ZIPL_LOAD_ADDR                  0x009000UL
59 #define ZIPL_FILENAME                   "s390-zipl.rom"
60
61 #define MAX_BLK_DEVS                    10
62
63 static VirtIOS390Bus *s390_bus;
64 static CPUState **ipi_states;
65
66 CPUState *s390_cpu_addr2state(uint16_t cpu_addr)
67 {
68     if (cpu_addr >= smp_cpus) {
69         return NULL;
70     }
71
72     return ipi_states[cpu_addr];
73 }
74
75 int s390_virtio_hypercall(CPUState *env, uint64_t mem, uint64_t hypercall)
76 {
77     int r = 0, i;
78
79     dprintf("KVM hypercall: %ld\n", hypercall);
80     switch (hypercall) {
81     case KVM_S390_VIRTIO_NOTIFY:
82         if (mem > ram_size) {
83             VirtIOS390Device *dev = s390_virtio_bus_find_vring(s390_bus,
84                                                                mem, &i);
85             if (dev) {
86                 virtio_queue_notify(dev->vdev, i);
87             } else {
88                 r = -EINVAL;
89             }
90         } else {
91             /* Early printk */
92         }
93         break;
94     case KVM_S390_VIRTIO_RESET:
95     {
96         VirtIOS390Device *dev;
97
98         dev = s390_virtio_bus_find_mem(s390_bus, mem);
99         virtio_reset(dev->vdev);
100         s390_virtio_device_sync(dev);
101         break;
102     }
103     case KVM_S390_VIRTIO_SET_STATUS:
104     {
105         VirtIOS390Device *dev;
106
107         dev = s390_virtio_bus_find_mem(s390_bus, mem);
108         if (dev) {
109             s390_virtio_device_update_status(dev);
110         } else {
111             r = -EINVAL;
112         }
113         break;
114     }
115     default:
116         r = -EINVAL;
117         break;
118     }
119
120     return r;
121 }
122
123 /* PC hardware initialisation */
124 static void s390_init(ram_addr_t my_ram_size,
125                       const char *boot_device,
126                       const char *kernel_filename,
127                       const char *kernel_cmdline,
128                       const char *initrd_filename,
129                       const char *cpu_model)
130 {
131     CPUState *env = NULL;
132     MemoryRegion *sysmem = get_system_memory();
133     MemoryRegion *ram = g_new(MemoryRegion, 1);
134     ram_addr_t kernel_size = 0;
135     ram_addr_t initrd_offset;
136     ram_addr_t initrd_size = 0;
137     int shift = 0;
138     uint8_t *storage_keys;
139     int i;
140
141     /* s390x ram size detection needs a 16bit multiplier + an increment. So
142        guests > 64GB can be specified in 2MB steps etc. */
143     while ((my_ram_size >> (20 + shift)) > 65535) {
144         shift++;
145     }
146     my_ram_size = my_ram_size >> (20 + shift) << (20 + shift);
147
148     /* lets propagate the changed ram size into the global variable. */
149     ram_size = my_ram_size;
150
151     /* get a BUS */
152     s390_bus = s390_virtio_bus_init(&my_ram_size);
153
154     /* allocate RAM */
155     memory_region_init_ram(ram, NULL, "s390.ram", my_ram_size);
156     memory_region_add_subregion(sysmem, 0, ram);
157
158     /* allocate storage keys */
159     storage_keys = g_malloc0(my_ram_size / TARGET_PAGE_SIZE);
160
161     /* init CPUs */
162     if (cpu_model == NULL) {
163         cpu_model = "host";
164     }
165
166     ipi_states = g_malloc(sizeof(CPUState *) * smp_cpus);
167
168     for (i = 0; i < smp_cpus; i++) {
169         CPUState *tmp_env;
170
171         tmp_env = cpu_init(cpu_model);
172         if (!env) {
173             env = tmp_env;
174         }
175         ipi_states[i] = tmp_env;
176         tmp_env->halted = 1;
177         tmp_env->exception_index = EXCP_HLT;
178         tmp_env->storage_keys = storage_keys;
179     }
180
181     env->halted = 0;
182     env->exception_index = 0;
183
184     if (kernel_filename) {
185         kernel_size = load_image(kernel_filename, qemu_get_ram_ptr(0));
186
187         if (lduw_be_phys(KERN_IMAGE_START) != 0x0dd0) {
188             fprintf(stderr, "Specified image is not an s390 boot image\n");
189             exit(1);
190         }
191
192         env->psw.addr = KERN_IMAGE_START;
193         env->psw.mask = 0x0000000180000000ULL;
194     } else {
195         ram_addr_t bios_size = 0;
196         char *bios_filename;
197
198         /* Load zipl bootloader */
199         if (bios_name == NULL) {
200             bios_name = ZIPL_FILENAME;
201         }
202
203         bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
204         bios_size = load_image(bios_filename, qemu_get_ram_ptr(ZIPL_LOAD_ADDR));
205         g_free(bios_filename);
206
207         if ((long)bios_size < 0) {
208             hw_error("could not load bootloader '%s'\n", bios_name);
209         }
210
211         if (bios_size > 4096) {
212             hw_error("stage1 bootloader is > 4k\n");
213         }
214
215         env->psw.addr = ZIPL_START;
216         env->psw.mask = 0x0000000180000000ULL;
217     }
218
219     if (initrd_filename) {
220         initrd_offset = INITRD_START;
221         while (kernel_size + 0x100000 > initrd_offset) {
222             initrd_offset += 0x100000;
223         }
224         initrd_size = load_image(initrd_filename, qemu_get_ram_ptr(initrd_offset));
225
226         stq_be_phys(INITRD_PARM_START, initrd_offset);
227         stq_be_phys(INITRD_PARM_SIZE, initrd_size);
228     }
229
230     if (kernel_cmdline) {
231         cpu_physical_memory_write(KERN_PARM_AREA, kernel_cmdline,
232                                   strlen(kernel_cmdline));
233     }
234
235     /* Create VirtIO network adapters */
236     for(i = 0; i < nb_nics; i++) {
237         NICInfo *nd = &nd_table[i];
238         DeviceState *dev;
239
240         if (!nd->model) {
241             nd->model = g_strdup("virtio");
242         }
243
244         if (strcmp(nd->model, "virtio")) {
245             fprintf(stderr, "S390 only supports VirtIO nics\n");
246             exit(1);
247         }
248
249         dev = qdev_create((BusState *)s390_bus, "virtio-net-s390");
250         qdev_set_nic_properties(dev, nd);
251         qdev_init_nofail(dev);
252     }
253
254     /* Create VirtIO disk drives */
255     for(i = 0; i < MAX_BLK_DEVS; i++) {
256         DriveInfo *dinfo;
257         DeviceState *dev;
258
259         dinfo = drive_get(IF_IDE, 0, i);
260         if (!dinfo) {
261             continue;
262         }
263
264         dev = qdev_create((BusState *)s390_bus, "virtio-blk-s390");
265         qdev_prop_set_drive_nofail(dev, "drive", dinfo->bdrv);
266         qdev_init_nofail(dev);
267     }
268 }
269
270 static QEMUMachine s390_machine = {
271     .name = "s390-virtio",
272     .alias = "s390",
273     .desc = "VirtIO based S390 machine",
274     .init = s390_init,
275     .no_serial = 1,
276     .no_parallel = 1,
277     .use_virtcon = 1,
278     .no_vga = 1,
279     .max_cpus = 255,
280     .is_default = 1,
281 };
282
283 static void s390_machine_init(void)
284 {
285     qemu_register_machine(&s390_machine);
286 }
287
288 machine_init(s390_machine_init);
This page took 0.040484 seconds and 4 git commands to generate.