xref: /qemu/hw/s390x/sclp.c (revision 72ac97cd)
1 /*
2  * SCLP Support
3  *
4  * Copyright IBM, Corp. 2012
5  *
6  * Authors:
7  *  Christian Borntraeger <borntraeger@de.ibm.com>
8  *  Heinz Graalfs <graalfs@linux.vnet.ibm.com>
9  *
10  * This work is licensed under the terms of the GNU GPL, version 2 or (at your
11  * option) any later version.  See the COPYING file in the top-level directory.
12  *
13  */
14 
15 #include "cpu.h"
16 #include "sysemu/kvm.h"
17 #include "exec/memory.h"
18 #include "sysemu/sysemu.h"
19 
20 #include "hw/s390x/sclp.h"
21 #include "hw/s390x/event-facility.h"
22 
23 static inline SCLPEventFacility *get_event_facility(void)
24 {
25     ObjectProperty *op = object_property_find(qdev_get_machine(),
26                                               TYPE_SCLP_EVENT_FACILITY,
27                                               NULL);
28     assert(op);
29     return op->opaque;
30 }
31 
32 /* Provide information about the configuration, CPUs and storage */
33 static void read_SCP_info(SCCB *sccb)
34 {
35     ReadInfo *read_info = (ReadInfo *) sccb;
36     CPUState *cpu;
37     int shift = 0;
38     int cpu_count = 0;
39     int i = 0;
40 
41     CPU_FOREACH(cpu) {
42         cpu_count++;
43     }
44 
45     /* CPU information */
46     read_info->entries_cpu = cpu_to_be16(cpu_count);
47     read_info->offset_cpu = cpu_to_be16(offsetof(ReadInfo, entries));
48     read_info->highest_cpu = cpu_to_be16(max_cpus);
49 
50     for (i = 0; i < cpu_count; i++) {
51         read_info->entries[i].address = i;
52         read_info->entries[i].type = 0;
53     }
54 
55     read_info->facilities = cpu_to_be64(SCLP_HAS_CPU_INFO);
56 
57     while ((ram_size >> (20 + shift)) > 65535) {
58         shift++;
59     }
60     read_info->rnmax = cpu_to_be16(ram_size >> (20 + shift));
61     read_info->rnsize = 1 << shift;
62     sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
63 }
64 
65 /* Provide information about the CPU */
66 static void sclp_read_cpu_info(SCCB *sccb)
67 {
68     ReadCpuInfo *cpu_info = (ReadCpuInfo *) sccb;
69     CPUState *cpu;
70     int cpu_count = 0;
71     int i = 0;
72 
73     CPU_FOREACH(cpu) {
74         cpu_count++;
75     }
76 
77     cpu_info->nr_configured = cpu_to_be16(cpu_count);
78     cpu_info->offset_configured = cpu_to_be16(offsetof(ReadCpuInfo, entries));
79     cpu_info->nr_standby = cpu_to_be16(0);
80 
81     /* The standby offset is 16-byte for each CPU */
82     cpu_info->offset_standby = cpu_to_be16(cpu_info->offset_configured
83         + cpu_info->nr_configured*sizeof(CPUEntry));
84 
85     for (i = 0; i < cpu_count; i++) {
86         cpu_info->entries[i].address = i;
87         cpu_info->entries[i].type = 0;
88     }
89 
90     sccb->h.response_code = cpu_to_be16(SCLP_RC_NORMAL_READ_COMPLETION);
91 }
92 
93 static void sclp_execute(SCCB *sccb, uint32_t code)
94 {
95     SCLPEventFacility *ef = get_event_facility();
96     SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
97 
98     switch (code & SCLP_CMD_CODE_MASK) {
99     case SCLP_CMDW_READ_SCP_INFO:
100     case SCLP_CMDW_READ_SCP_INFO_FORCED:
101         read_SCP_info(sccb);
102         break;
103     case SCLP_CMDW_READ_CPU_INFO:
104         sclp_read_cpu_info(sccb);
105         break;
106     default:
107         efc->command_handler(ef, sccb, code);
108         break;
109     }
110 }
111 
112 int sclp_service_call(CPUS390XState *env, uint64_t sccb, uint32_t code)
113 {
114     int r = 0;
115     SCCB work_sccb;
116 
117     hwaddr sccb_len = sizeof(SCCB);
118 
119     /* first some basic checks on program checks */
120     if (env->psw.mask & PSW_MASK_PSTATE) {
121         r = -PGM_PRIVILEGED;
122         goto out;
123     }
124     if (cpu_physical_memory_is_io(sccb)) {
125         r = -PGM_ADDRESSING;
126         goto out;
127     }
128     if ((sccb & ~0x1fffUL) == 0 || (sccb & ~0x1fffUL) == env->psa
129         || (sccb & ~0x7ffffff8UL) != 0) {
130         r = -PGM_SPECIFICATION;
131         goto out;
132     }
133 
134     /*
135      * we want to work on a private copy of the sccb, to prevent guests
136      * from playing dirty tricks by modifying the memory content after
137      * the host has checked the values
138      */
139     cpu_physical_memory_read(sccb, &work_sccb, sccb_len);
140 
141     /* Valid sccb sizes */
142     if (be16_to_cpu(work_sccb.h.length) < sizeof(SCCBHeader) ||
143         be16_to_cpu(work_sccb.h.length) > SCCB_SIZE) {
144         r = -PGM_SPECIFICATION;
145         goto out;
146     }
147 
148     sclp_execute((SCCB *)&work_sccb, code);
149 
150     cpu_physical_memory_write(sccb, &work_sccb,
151                               be16_to_cpu(work_sccb.h.length));
152 
153     sclp_service_interrupt(sccb);
154 
155 out:
156     return r;
157 }
158 
159 void sclp_service_interrupt(uint32_t sccb)
160 {
161     SCLPEventFacility *ef = get_event_facility();
162     SCLPEventFacilityClass *efc = EVENT_FACILITY_GET_CLASS(ef);
163 
164     uint32_t param = sccb & ~3;
165 
166     /* Indicate whether an event is still pending */
167     param |= efc->event_pending(ef) ? 1 : 0;
168 
169     if (!param) {
170         /* No need to send an interrupt, there's nothing to be notified about */
171         return;
172     }
173     s390_sclp_extint(param);
174 }
175 
176 /* qemu object creation and initialization functions */
177 
178 void s390_sclp_init(void)
179 {
180     DeviceState *dev  = qdev_create(NULL, TYPE_SCLP_EVENT_FACILITY);
181 
182     object_property_add_child(qdev_get_machine(), TYPE_SCLP_EVENT_FACILITY,
183                               OBJECT(dev), NULL);
184     qdev_init_nofail(dev);
185 }
186