Add qemu 2.4.0
[kvmfornfv.git] / qemu / hw / s390x / ipl.c
1 /*
2  * bootloader support
3  *
4  * Copyright IBM, Corp. 2012
5  *
6  * Authors:
7  *  Christian Borntraeger <borntraeger@de.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or (at your
10  * option) any later version.  See the COPYING file in the top-level directory.
11  *
12  */
13
14 #include "sysemu/sysemu.h"
15 #include "cpu.h"
16 #include "elf.h"
17 #include "hw/loader.h"
18 #include "hw/sysbus.h"
19 #include "hw/s390x/virtio-ccw.h"
20 #include "hw/s390x/css.h"
21 #include "ipl.h"
22
23 #define KERN_IMAGE_START                0x010000UL
24 #define KERN_PARM_AREA                  0x010480UL
25 #define INITRD_START                    0x800000UL
26 #define INITRD_PARM_START               0x010408UL
27 #define INITRD_PARM_SIZE                0x010410UL
28 #define PARMFILE_START                  0x001000UL
29 #define ZIPL_IMAGE_START                0x009000UL
30 #define IPL_PSW_MASK                    (PSW_MASK_32 | PSW_MASK_64)
31
32 #define TYPE_S390_IPL "s390-ipl"
33 #define S390_IPL(obj) \
34     OBJECT_CHECK(S390IPLState, (obj), TYPE_S390_IPL)
35 #if 0
36 #define S390_IPL_CLASS(klass) \
37     OBJECT_CLASS_CHECK(S390IPLState, (klass), TYPE_S390_IPL)
38 #define S390_IPL_GET_CLASS(obj) \
39     OBJECT_GET_CLASS(S390IPLState, (obj), TYPE_S390_IPL)
40 #endif
41
42 typedef struct S390IPLClass {
43     /*< private >*/
44     SysBusDeviceClass parent_class;
45     /*< public >*/
46
47     void (*parent_reset) (SysBusDevice *dev);
48 } S390IPLClass;
49
50 typedef struct S390IPLState {
51     /*< private >*/
52     SysBusDevice parent_obj;
53     uint64_t start_addr;
54     uint64_t bios_start_addr;
55     bool enforce_bios;
56     IplParameterBlock iplb;
57     bool iplb_valid;
58     bool reipl_requested;
59
60     /*< public >*/
61     char *kernel;
62     char *initrd;
63     char *cmdline;
64     char *firmware;
65     uint8_t cssid;
66     uint8_t ssid;
67     uint16_t devno;
68 } S390IPLState;
69
70 static const VMStateDescription vmstate_iplb = {
71     .name = "ipl/iplb",
72     .version_id = 0,
73     .minimum_version_id = 0,
74     .fields = (VMStateField[]) {
75         VMSTATE_UINT8_ARRAY(reserved1, IplParameterBlock, 110),
76         VMSTATE_UINT16(devno, IplParameterBlock),
77         VMSTATE_UINT8_ARRAY(reserved2, IplParameterBlock, 88),
78         VMSTATE_END_OF_LIST()
79     }
80 };
81
82 static const VMStateDescription vmstate_ipl = {
83     .name = "ipl",
84     .version_id = 0,
85     .minimum_version_id = 0,
86     .fields = (VMStateField[]) {
87         VMSTATE_UINT64(start_addr, S390IPLState),
88         VMSTATE_UINT64(bios_start_addr, S390IPLState),
89         VMSTATE_STRUCT(iplb, S390IPLState, 0, vmstate_iplb, IplParameterBlock),
90         VMSTATE_BOOL(iplb_valid, S390IPLState),
91         VMSTATE_UINT8(cssid, S390IPLState),
92         VMSTATE_UINT8(ssid, S390IPLState),
93         VMSTATE_UINT16(devno, S390IPLState),
94         VMSTATE_END_OF_LIST()
95      }
96 };
97
98 static uint64_t bios_translate_addr(void *opaque, uint64_t srcaddr)
99 {
100     uint64_t dstaddr = *(uint64_t *) opaque;
101     /*
102      * Assuming that our s390-ccw.img was linked for starting at address 0,
103      * we can simply add the destination address for the final location
104      */
105     return srcaddr + dstaddr;
106 }
107
108 static int s390_ipl_init(SysBusDevice *dev)
109 {
110     S390IPLState *ipl = S390_IPL(dev);
111     uint64_t pentry = KERN_IMAGE_START;
112     int kernel_size;
113
114     int bios_size;
115     char *bios_filename;
116
117     /*
118      * Always load the bios if it was enforced,
119      * even if an external kernel has been defined.
120      */
121     if (!ipl->kernel || ipl->enforce_bios) {
122         uint64_t fwbase = (MIN(ram_size, 0x80000000U) - 0x200000) & ~0xffffUL;
123
124         if (bios_name == NULL) {
125             bios_name = ipl->firmware;
126         }
127
128         bios_filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
129         if (bios_filename == NULL) {
130             hw_error("could not find stage1 bootloader\n");
131         }
132
133         bios_size = load_elf(bios_filename, bios_translate_addr, &fwbase,
134                              &ipl->bios_start_addr, NULL, NULL, 1,
135                              ELF_MACHINE, 0);
136         if (bios_size > 0) {
137             /* Adjust ELF start address to final location */
138             ipl->bios_start_addr += fwbase;
139         } else {
140             /* Try to load non-ELF file (e.g. s390-zipl.rom) */
141             bios_size = load_image_targphys(bios_filename, ZIPL_IMAGE_START,
142                                             4096);
143             ipl->bios_start_addr = ZIPL_IMAGE_START;
144         }
145         g_free(bios_filename);
146
147         if (bios_size == -1) {
148             hw_error("could not load bootloader '%s'\n", bios_name);
149         }
150
151         /* default boot target is the bios */
152         ipl->start_addr = ipl->bios_start_addr;
153     }
154
155     if (ipl->kernel) {
156         kernel_size = load_elf(ipl->kernel, NULL, NULL, &pentry, NULL,
157                                NULL, 1, ELF_MACHINE, 0);
158         if (kernel_size < 0) {
159             kernel_size = load_image_targphys(ipl->kernel, 0, ram_size);
160         }
161         if (kernel_size < 0) {
162             fprintf(stderr, "could not load kernel '%s'\n", ipl->kernel);
163             return -1;
164         }
165         /*
166          * Is it a Linux kernel (starting at 0x10000)? If yes, we fill in the
167          * kernel parameters here as well. Note: For old kernels (up to 3.2)
168          * we can not rely on the ELF entry point - it was 0x800 (the SALIPL
169          * loader) and it won't work. For this case we force it to 0x10000, too.
170          */
171         if (pentry == KERN_IMAGE_START || pentry == 0x800) {
172             ipl->start_addr = KERN_IMAGE_START;
173             /* Overwrite parameters in the kernel image, which are "rom" */
174             strcpy(rom_ptr(KERN_PARM_AREA), ipl->cmdline);
175         } else {
176             ipl->start_addr = pentry;
177         }
178
179         if (ipl->initrd) {
180             ram_addr_t initrd_offset;
181             int initrd_size;
182
183             initrd_offset = INITRD_START;
184             while (kernel_size + 0x100000 > initrd_offset) {
185                 initrd_offset += 0x100000;
186             }
187             initrd_size = load_image_targphys(ipl->initrd, initrd_offset,
188                                               ram_size - initrd_offset);
189             if (initrd_size == -1) {
190                 fprintf(stderr, "qemu: could not load initrd '%s'\n",
191                         ipl->initrd);
192                 exit(1);
193             }
194
195             /*
196              * we have to overwrite values in the kernel image,
197              * which are "rom"
198              */
199             stq_p(rom_ptr(INITRD_PARM_START), initrd_offset);
200             stq_p(rom_ptr(INITRD_PARM_SIZE), initrd_size);
201         }
202     }
203     return 0;
204 }
205
206 static Property s390_ipl_properties[] = {
207     DEFINE_PROP_STRING("kernel", S390IPLState, kernel),
208     DEFINE_PROP_STRING("initrd", S390IPLState, initrd),
209     DEFINE_PROP_STRING("cmdline", S390IPLState, cmdline),
210     DEFINE_PROP_STRING("firmware", S390IPLState, firmware),
211     DEFINE_PROP_BOOL("enforce_bios", S390IPLState, enforce_bios, false),
212     DEFINE_PROP_END_OF_LIST(),
213 };
214
215 /*
216  * In addition to updating the iplstate, this function returns:
217  * - 0 if system was ipled with external kernel
218  * - -1 if no valid boot device was found
219  * - ccw id of the boot device otherwise
220  */
221 static uint64_t s390_update_iplstate(CPUS390XState *env, S390IPLState *ipl)
222 {
223     DeviceState *dev_st;
224
225     if (ipl->iplb_valid) {
226         ipl->cssid = 0;
227         ipl->ssid = 0;
228         ipl->devno = ipl->iplb.devno;
229         goto out;
230     }
231
232     if (ipl->kernel) {
233         return 0;
234     }
235
236     dev_st = get_boot_device(0);
237     if (dev_st) {
238         VirtioCcwDevice *ccw_dev = (VirtioCcwDevice *) object_dynamic_cast(
239             OBJECT(qdev_get_parent_bus(dev_st)->parent),
240                 TYPE_VIRTIO_CCW_DEVICE);
241         if (ccw_dev) {
242             ipl->cssid = ccw_dev->sch->cssid;
243             ipl->ssid = ccw_dev->sch->ssid;
244             ipl->devno = ccw_dev->sch->devno;
245             goto out;
246         }
247     }
248
249     return -1;
250 out:
251     return (uint32_t) (ipl->cssid << 24 | ipl->ssid << 16 | ipl->devno);
252 }
253
254 int s390_ipl_update_diag308(IplParameterBlock *iplb)
255 {
256     S390IPLState *ipl;
257
258     ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
259     if (ipl) {
260         ipl->iplb = *iplb;
261         ipl->iplb_valid = true;
262         return 0;
263     }
264     return -1;
265 }
266
267 IplParameterBlock *s390_ipl_get_iplb(void)
268 {
269     S390IPLState *ipl;
270
271     ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
272     if (!ipl || !ipl->iplb_valid) {
273         return NULL;
274     }
275     return &ipl->iplb;
276 }
277
278 void s390_reipl_request(void)
279 {
280     S390IPLState *ipl;
281
282     ipl = S390_IPL(object_resolve_path(TYPE_S390_IPL, NULL));
283     ipl->reipl_requested = true;
284     qemu_system_reset_request();
285 }
286
287 static void s390_ipl_reset(DeviceState *dev)
288 {
289     S390IPLState *ipl = S390_IPL(dev);
290     S390CPU *cpu = S390_CPU(qemu_get_cpu(0));
291     CPUS390XState *env = &cpu->env;
292
293     env->psw.addr = ipl->start_addr;
294     env->psw.mask = IPL_PSW_MASK;
295
296     if (!ipl->reipl_requested) {
297         ipl->iplb_valid = false;
298     }
299     ipl->reipl_requested = false;
300
301     if (!ipl->kernel || ipl->iplb_valid) {
302         env->psw.addr = ipl->bios_start_addr;
303         env->regs[7] = s390_update_iplstate(env, ipl);
304     }
305
306     s390_cpu_set_state(CPU_STATE_OPERATING, cpu);
307 }
308
309 static void s390_ipl_class_init(ObjectClass *klass, void *data)
310 {
311     DeviceClass *dc = DEVICE_CLASS(klass);
312     SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
313
314     k->init = s390_ipl_init;
315     dc->props = s390_ipl_properties;
316     dc->reset = s390_ipl_reset;
317     dc->vmsd = &vmstate_ipl;
318     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
319 }
320
321 static const TypeInfo s390_ipl_info = {
322     .class_init = s390_ipl_class_init,
323     .parent = TYPE_SYS_BUS_DEVICE,
324     .name  = "s390-ipl",
325     .instance_size  = sizeof(S390IPLState),
326 };
327
328 static void s390_ipl_register_types(void)
329 {
330     type_register_static(&s390_ipl_info);
331 }
332
333 type_init(s390_ipl_register_types)