xref: /openbmc/qemu/target/s390x/helper.c (revision 9e34f127f419b3941b36dfdfac79640dc81e97e2)
1fcf5ef2aSThomas Huth /*
2da944885SCho, Yu-Chen  *  S/390 helpers - sysemu only
3fcf5ef2aSThomas Huth  *
4fcf5ef2aSThomas Huth  *  Copyright (c) 2009 Ulrich Hecht
5fcf5ef2aSThomas Huth  *  Copyright (c) 2011 Alexander Graf
6fcf5ef2aSThomas Huth  *
7fcf5ef2aSThomas Huth  * This library is free software; you can redistribute it and/or
8fcf5ef2aSThomas Huth  * modify it under the terms of the GNU Lesser General Public
9fcf5ef2aSThomas Huth  * License as published by the Free Software Foundation; either
1041c6a6ddSThomas Huth  * version 2.1 of the License, or (at your option) any later version.
11fcf5ef2aSThomas Huth  *
12fcf5ef2aSThomas Huth  * This library is distributed in the hope that it will be useful,
13fcf5ef2aSThomas Huth  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14fcf5ef2aSThomas Huth  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15fcf5ef2aSThomas Huth  * Lesser General Public License for more details.
16fcf5ef2aSThomas Huth  *
17fcf5ef2aSThomas Huth  * You should have received a copy of the GNU Lesser General Public
18fcf5ef2aSThomas Huth  * License along with this library; if not, see <http://www.gnu.org/licenses/>.
19fcf5ef2aSThomas Huth  */
20fcf5ef2aSThomas Huth 
21fcf5ef2aSThomas Huth #include "qemu/osdep.h"
22fcf5ef2aSThomas Huth #include "cpu.h"
23b6b47223SCho, Yu-Chen #include "s390x-internal.h"
244ea5fe99SAlex Bennée #include "gdbstub/helpers.h"
25fcf5ef2aSThomas Huth #include "qemu/timer.h"
26fcf5ef2aSThomas Huth #include "hw/s390x/ioinst.h"
27f5f9c6eaSPhilippe Mathieu-Daudé #include "target/s390x/kvm/pv.h"
2883f7f329SDavid Hildenbrand #include "sysemu/hw_accel.h"
2954d31236SMarkus Armbruster #include "sysemu/runstate.h"
30fcf5ef2aSThomas Huth 
s390x_tod_timer(void * opaque)31fcf5ef2aSThomas Huth void s390x_tod_timer(void *opaque)
32fcf5ef2aSThomas Huth {
336482b0ffSDavid Hildenbrand     cpu_inject_clock_comparator((S390CPU *) opaque);
34fcf5ef2aSThomas Huth }
35fcf5ef2aSThomas Huth 
s390x_cpu_timer(void * opaque)36fcf5ef2aSThomas Huth void s390x_cpu_timer(void *opaque)
37fcf5ef2aSThomas Huth {
386482b0ffSDavid Hildenbrand     cpu_inject_cpu_timer((S390CPU *) opaque);
39fcf5ef2aSThomas Huth }
40fcf5ef2aSThomas Huth 
s390_cpu_get_phys_page_debug(CPUState * cs,vaddr vaddr)41fcf5ef2aSThomas Huth hwaddr s390_cpu_get_phys_page_debug(CPUState *cs, vaddr vaddr)
42fcf5ef2aSThomas Huth {
43fcf5ef2aSThomas Huth     S390CPU *cpu = S390_CPU(cs);
44fcf5ef2aSThomas Huth     CPUS390XState *env = &cpu->env;
45fcf5ef2aSThomas Huth     target_ulong raddr;
46fcf5ef2aSThomas Huth     int prot;
47fcf5ef2aSThomas Huth     uint64_t asc = env->psw.mask & PSW_MASK_ASC;
48ce7ac79dSRichard Henderson     uint64_t tec;
49fcf5ef2aSThomas Huth 
50fcf5ef2aSThomas Huth     /* 31-Bit mode */
51fcf5ef2aSThomas Huth     if (!(env->psw.mask & PSW_MASK_64)) {
52fcf5ef2aSThomas Huth         vaddr &= 0x7fffffff;
53fcf5ef2aSThomas Huth     }
54fcf5ef2aSThomas Huth 
55c36709e4SDavid Hildenbrand     /* We want to read the code (e.g., see what we are single-stepping).*/
56c36709e4SDavid Hildenbrand     if (asc != PSW_ASC_HOME) {
57c36709e4SDavid Hildenbrand         asc = PSW_ASC_PRIMARY;
58c36709e4SDavid Hildenbrand     }
59c36709e4SDavid Hildenbrand 
603a06f981SDavid Hildenbrand     /*
613a06f981SDavid Hildenbrand      * We want to read code even if IEP is active. Use MMU_DATA_LOAD instead
623a06f981SDavid Hildenbrand      * of MMU_INST_FETCH.
633a06f981SDavid Hildenbrand      */
64ce7ac79dSRichard Henderson     if (mmu_translate(env, vaddr, MMU_DATA_LOAD, asc, &raddr, &prot, &tec)) {
65fcf5ef2aSThomas Huth         return -1;
66fcf5ef2aSThomas Huth     }
67fcf5ef2aSThomas Huth     return raddr;
68fcf5ef2aSThomas Huth }
69fcf5ef2aSThomas Huth 
s390_cpu_get_phys_addr_debug(CPUState * cs,vaddr vaddr)70fcf5ef2aSThomas Huth hwaddr s390_cpu_get_phys_addr_debug(CPUState *cs, vaddr vaddr)
71fcf5ef2aSThomas Huth {
72fcf5ef2aSThomas Huth     hwaddr phys_addr;
73fcf5ef2aSThomas Huth     target_ulong page;
74fcf5ef2aSThomas Huth 
75fcf5ef2aSThomas Huth     page = vaddr & TARGET_PAGE_MASK;
76fcf5ef2aSThomas Huth     phys_addr = cpu_get_phys_page_debug(cs, page);
77fcf5ef2aSThomas Huth     phys_addr += (vaddr & ~TARGET_PAGE_MASK);
78fcf5ef2aSThomas Huth 
79fcf5ef2aSThomas Huth     return phys_addr;
80fcf5ef2aSThomas Huth }
81fcf5ef2aSThomas Huth 
is_special_wait_psw(uint64_t psw_addr)8283f7f329SDavid Hildenbrand static inline bool is_special_wait_psw(uint64_t psw_addr)
8383f7f329SDavid Hildenbrand {
8483f7f329SDavid Hildenbrand     /* signal quiesce */
858b51c096SChristian Borntraeger     return (psw_addr & 0xfffUL) == 0xfffUL;
8683f7f329SDavid Hildenbrand }
8783f7f329SDavid Hildenbrand 
s390_handle_wait(S390CPU * cpu)8883f7f329SDavid Hildenbrand void s390_handle_wait(S390CPU *cpu)
8983f7f329SDavid Hildenbrand {
904ada99adSChristian Borntraeger     CPUState *cs = CPU(cpu);
914ada99adSChristian Borntraeger 
9283f7f329SDavid Hildenbrand     if (s390_cpu_halt(cpu) == 0) {
9383f7f329SDavid Hildenbrand         if (is_special_wait_psw(cpu->env.psw.addr)) {
9483f7f329SDavid Hildenbrand             qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
9583f7f329SDavid Hildenbrand         } else {
964ada99adSChristian Borntraeger             cpu->env.crash_reason = S390_CRASH_REASON_DISABLED_WAIT;
974ada99adSChristian Borntraeger             qemu_system_guest_panicked(cpu_get_crash_info(cs));
9883f7f329SDavid Hildenbrand         }
9983f7f329SDavid Hildenbrand     }
10083f7f329SDavid Hildenbrand }
10183f7f329SDavid Hildenbrand 
cpu_map_lowcore(CPUS390XState * env)102cded4014SThomas Huth LowCore *cpu_map_lowcore(CPUS390XState *env)
103fcf5ef2aSThomas Huth {
104fcf5ef2aSThomas Huth     LowCore *lowcore;
105fcf5ef2aSThomas Huth     hwaddr len = sizeof(LowCore);
106fcf5ef2aSThomas Huth 
10785eb7c18SPhilippe Mathieu-Daudé     lowcore = cpu_physical_memory_map(env->psa, &len, true);
108fcf5ef2aSThomas Huth 
109fcf5ef2aSThomas Huth     if (len < sizeof(LowCore)) {
110dc79e928SRichard Henderson         cpu_abort(env_cpu(env), "Could not map lowcore\n");
111fcf5ef2aSThomas Huth     }
112fcf5ef2aSThomas Huth 
113fcf5ef2aSThomas Huth     return lowcore;
114fcf5ef2aSThomas Huth }
115fcf5ef2aSThomas Huth 
cpu_unmap_lowcore(LowCore * lowcore)116cded4014SThomas Huth void cpu_unmap_lowcore(LowCore *lowcore)
117fcf5ef2aSThomas Huth {
118fcf5ef2aSThomas Huth     cpu_physical_memory_unmap(lowcore, sizeof(LowCore), 1, sizeof(LowCore));
119fcf5ef2aSThomas Huth }
120fcf5ef2aSThomas Huth 
do_restart_interrupt(CPUS390XState * env)121fcf5ef2aSThomas Huth void do_restart_interrupt(CPUS390XState *env)
122fcf5ef2aSThomas Huth {
123fcf5ef2aSThomas Huth     uint64_t mask, addr;
124fcf5ef2aSThomas Huth     LowCore *lowcore;
125fcf5ef2aSThomas Huth 
126fcf5ef2aSThomas Huth     lowcore = cpu_map_lowcore(env);
127fcf5ef2aSThomas Huth 
128e2b2a864SRichard Henderson     lowcore->restart_old_psw.mask = cpu_to_be64(s390_cpu_get_psw_mask(env));
129fcf5ef2aSThomas Huth     lowcore->restart_old_psw.addr = cpu_to_be64(env->psw.addr);
130fcf5ef2aSThomas Huth     mask = be64_to_cpu(lowcore->restart_new_psw.mask);
131fcf5ef2aSThomas Huth     addr = be64_to_cpu(lowcore->restart_new_psw.addr);
132fcf5ef2aSThomas Huth 
133fcf5ef2aSThomas Huth     cpu_unmap_lowcore(lowcore);
134b1ab5f60SDavid Hildenbrand     env->pending_int &= ~INTERRUPT_RESTART;
135fcf5ef2aSThomas Huth 
136e2b2a864SRichard Henderson     s390_cpu_set_psw(env, mask, addr);
137fcf5ef2aSThomas Huth }
138fcf5ef2aSThomas Huth 
s390_cpu_recompute_watchpoints(CPUState * cs)139fcf5ef2aSThomas Huth void s390_cpu_recompute_watchpoints(CPUState *cs)
140fcf5ef2aSThomas Huth {
141fcf5ef2aSThomas Huth     const int wp_flags = BP_CPU | BP_MEM_WRITE | BP_STOP_BEFORE_ACCESS;
142*d0143fa9SPhilippe Mathieu-Daudé     CPUS390XState *env = cpu_env(cs);
143fcf5ef2aSThomas Huth 
144fcf5ef2aSThomas Huth     /* We are called when the watchpoints have changed. First
145fcf5ef2aSThomas Huth        remove them all.  */
146fcf5ef2aSThomas Huth     cpu_watchpoint_remove_all(cs, BP_CPU);
147fcf5ef2aSThomas Huth 
148fcf5ef2aSThomas Huth     /* Return if PER is not enabled */
149fcf5ef2aSThomas Huth     if (!(env->psw.mask & PSW_MASK_PER)) {
150fcf5ef2aSThomas Huth         return;
151fcf5ef2aSThomas Huth     }
152fcf5ef2aSThomas Huth 
153fcf5ef2aSThomas Huth     /* Return if storage-alteration event is not enabled.  */
154fcf5ef2aSThomas Huth     if (!(env->cregs[9] & PER_CR9_EVENT_STORE)) {
155fcf5ef2aSThomas Huth         return;
156fcf5ef2aSThomas Huth     }
157fcf5ef2aSThomas Huth 
158fcf5ef2aSThomas Huth     if (env->cregs[10] == 0 && env->cregs[11] == -1LL) {
159fcf5ef2aSThomas Huth         /* We can't create a watchoint spanning the whole memory range, so
160fcf5ef2aSThomas Huth            split it in two parts.   */
161fcf5ef2aSThomas Huth         cpu_watchpoint_insert(cs, 0, 1ULL << 63, wp_flags, NULL);
162fcf5ef2aSThomas Huth         cpu_watchpoint_insert(cs, 1ULL << 63, 1ULL << 63, wp_flags, NULL);
163fcf5ef2aSThomas Huth     } else if (env->cregs[10] > env->cregs[11]) {
164fcf5ef2aSThomas Huth         /* The address range loops, create two watchpoints.  */
165fcf5ef2aSThomas Huth         cpu_watchpoint_insert(cs, env->cregs[10], -env->cregs[10],
166fcf5ef2aSThomas Huth                               wp_flags, NULL);
167fcf5ef2aSThomas Huth         cpu_watchpoint_insert(cs, 0, env->cregs[11] + 1, wp_flags, NULL);
168fcf5ef2aSThomas Huth 
169fcf5ef2aSThomas Huth     } else {
170fcf5ef2aSThomas Huth         /* Default case, create a single watchpoint.  */
171fcf5ef2aSThomas Huth         cpu_watchpoint_insert(cs, env->cregs[10],
172fcf5ef2aSThomas Huth                               env->cregs[11] - env->cregs[10] + 1,
173fcf5ef2aSThomas Huth                               wp_flags, NULL);
174fcf5ef2aSThomas Huth     }
175fcf5ef2aSThomas Huth }
176fcf5ef2aSThomas Huth 
177257619beSDavid Hildenbrand typedef struct SigpSaveArea {
178cf729baaSDavid Hildenbrand     uint64_t    fprs[16];                       /* 0x0000 */
179cf729baaSDavid Hildenbrand     uint64_t    grs[16];                        /* 0x0080 */
180cf729baaSDavid Hildenbrand     PSW         psw;                            /* 0x0100 */
181cf729baaSDavid Hildenbrand     uint8_t     pad_0x0110[0x0118 - 0x0110];    /* 0x0110 */
182cf729baaSDavid Hildenbrand     uint32_t    prefix;                         /* 0x0118 */
183cf729baaSDavid Hildenbrand     uint32_t    fpc;                            /* 0x011c */
184cf729baaSDavid Hildenbrand     uint8_t     pad_0x0120[0x0124 - 0x0120];    /* 0x0120 */
185cf729baaSDavid Hildenbrand     uint32_t    todpr;                          /* 0x0124 */
186cf729baaSDavid Hildenbrand     uint64_t    cputm;                          /* 0x0128 */
187cf729baaSDavid Hildenbrand     uint64_t    ckc;                            /* 0x0130 */
188cf729baaSDavid Hildenbrand     uint8_t     pad_0x0138[0x0140 - 0x0138];    /* 0x0138 */
189cf729baaSDavid Hildenbrand     uint32_t    ars[16];                        /* 0x0140 */
190cf729baaSDavid Hildenbrand     uint64_t    crs[16];                        /* 0x0384 */
191257619beSDavid Hildenbrand } SigpSaveArea;
192257619beSDavid Hildenbrand QEMU_BUILD_BUG_ON(sizeof(SigpSaveArea) != 512);
193cf729baaSDavid Hildenbrand 
s390_store_status(S390CPU * cpu,hwaddr addr,bool store_arch)194cf729baaSDavid Hildenbrand int s390_store_status(S390CPU *cpu, hwaddr addr, bool store_arch)
195cf729baaSDavid Hildenbrand {
196cf729baaSDavid Hildenbrand     static const uint8_t ar_id = 1;
197257619beSDavid Hildenbrand     SigpSaveArea *sa;
198cf729baaSDavid Hildenbrand     hwaddr len = sizeof(*sa);
199cf729baaSDavid Hildenbrand     int i;
200cf729baaSDavid Hildenbrand 
201f2a2d9a2SJanosch Frank     /* For PVMs storing will occur when this cpu enters SIE again */
202f2a2d9a2SJanosch Frank     if (s390_is_pv()) {
203f2a2d9a2SJanosch Frank         return 0;
204f2a2d9a2SJanosch Frank     }
205f2a2d9a2SJanosch Frank 
20685eb7c18SPhilippe Mathieu-Daudé     sa = cpu_physical_memory_map(addr, &len, true);
207cf729baaSDavid Hildenbrand     if (!sa) {
208cf729baaSDavid Hildenbrand         return -EFAULT;
209cf729baaSDavid Hildenbrand     }
210cf729baaSDavid Hildenbrand     if (len != sizeof(*sa)) {
211cf729baaSDavid Hildenbrand         cpu_physical_memory_unmap(sa, len, 1, 0);
212cf729baaSDavid Hildenbrand         return -EFAULT;
213cf729baaSDavid Hildenbrand     }
214cf729baaSDavid Hildenbrand 
215cf729baaSDavid Hildenbrand     if (store_arch) {
216cf729baaSDavid Hildenbrand         cpu_physical_memory_write(offsetof(LowCore, ar_access_id), &ar_id, 1);
217cf729baaSDavid Hildenbrand     }
218cf729baaSDavid Hildenbrand     for (i = 0; i < 16; ++i) {
2194f83d7d2SDavid Hildenbrand         sa->fprs[i] = cpu_to_be64(*get_freg(&cpu->env, i));
220cf729baaSDavid Hildenbrand     }
221cf729baaSDavid Hildenbrand     for (i = 0; i < 16; ++i) {
222cf729baaSDavid Hildenbrand         sa->grs[i] = cpu_to_be64(cpu->env.regs[i]);
223cf729baaSDavid Hildenbrand     }
224cf729baaSDavid Hildenbrand     sa->psw.addr = cpu_to_be64(cpu->env.psw.addr);
225e2b2a864SRichard Henderson     sa->psw.mask = cpu_to_be64(s390_cpu_get_psw_mask(&cpu->env));
226cf729baaSDavid Hildenbrand     sa->prefix = cpu_to_be32(cpu->env.psa);
227cf729baaSDavid Hildenbrand     sa->fpc = cpu_to_be32(cpu->env.fpc);
228cf729baaSDavid Hildenbrand     sa->todpr = cpu_to_be32(cpu->env.todpr);
229cf729baaSDavid Hildenbrand     sa->cputm = cpu_to_be64(cpu->env.cputm);
230cf729baaSDavid Hildenbrand     sa->ckc = cpu_to_be64(cpu->env.ckc >> 8);
231cf729baaSDavid Hildenbrand     for (i = 0; i < 16; ++i) {
232cf729baaSDavid Hildenbrand         sa->ars[i] = cpu_to_be32(cpu->env.aregs[i]);
233cf729baaSDavid Hildenbrand     }
234cf729baaSDavid Hildenbrand     for (i = 0; i < 16; ++i) {
235dc0bbef5SDavid Hildenbrand         sa->crs[i] = cpu_to_be64(cpu->env.cregs[i]);
236cf729baaSDavid Hildenbrand     }
237cf729baaSDavid Hildenbrand 
238cf729baaSDavid Hildenbrand     cpu_physical_memory_unmap(sa, len, 1, len);
239cf729baaSDavid Hildenbrand 
240cf729baaSDavid Hildenbrand     return 0;
241cf729baaSDavid Hildenbrand }
242f875cb0cSDavid Hildenbrand 
2432cca53fdSDavid Hildenbrand typedef struct SigpAdtlSaveArea {
2442cca53fdSDavid Hildenbrand     uint64_t    vregs[32][2];                     /* 0x0000 */
2452cca53fdSDavid Hildenbrand     uint8_t     pad_0x0200[0x0400 - 0x0200];      /* 0x0200 */
2462cca53fdSDavid Hildenbrand     uint64_t    gscb[4];                          /* 0x0400 */
2472cca53fdSDavid Hildenbrand     uint8_t     pad_0x0420[0x1000 - 0x0420];      /* 0x0420 */
2482cca53fdSDavid Hildenbrand } SigpAdtlSaveArea;
2492cca53fdSDavid Hildenbrand QEMU_BUILD_BUG_ON(sizeof(SigpAdtlSaveArea) != 4096);
2502cca53fdSDavid Hildenbrand 
251f875cb0cSDavid Hildenbrand #define ADTL_GS_MIN_SIZE 2048 /* minimal size of adtl save area for GS */
s390_store_adtl_status(S390CPU * cpu,hwaddr addr,hwaddr len)252f875cb0cSDavid Hildenbrand int s390_store_adtl_status(S390CPU *cpu, hwaddr addr, hwaddr len)
253f875cb0cSDavid Hildenbrand {
2542cca53fdSDavid Hildenbrand     SigpAdtlSaveArea *sa;
255f875cb0cSDavid Hildenbrand     hwaddr save = len;
2562cca53fdSDavid Hildenbrand     int i;
257f875cb0cSDavid Hildenbrand 
25885eb7c18SPhilippe Mathieu-Daudé     sa = cpu_physical_memory_map(addr, &save, true);
2592cca53fdSDavid Hildenbrand     if (!sa) {
260f875cb0cSDavid Hildenbrand         return -EFAULT;
261f875cb0cSDavid Hildenbrand     }
262f875cb0cSDavid Hildenbrand     if (save != len) {
2632cca53fdSDavid Hildenbrand         cpu_physical_memory_unmap(sa, len, 1, 0);
264f875cb0cSDavid Hildenbrand         return -EFAULT;
265f875cb0cSDavid Hildenbrand     }
266f875cb0cSDavid Hildenbrand 
267f875cb0cSDavid Hildenbrand     if (s390_has_feat(S390_FEAT_VECTOR)) {
2682cca53fdSDavid Hildenbrand         for (i = 0; i < 32; i++) {
2694f83d7d2SDavid Hildenbrand             sa->vregs[i][0] = cpu_to_be64(cpu->env.vregs[i][0]);
2704f83d7d2SDavid Hildenbrand             sa->vregs[i][1] = cpu_to_be64(cpu->env.vregs[i][1]);
2712cca53fdSDavid Hildenbrand         }
272f875cb0cSDavid Hildenbrand     }
273f875cb0cSDavid Hildenbrand     if (s390_has_feat(S390_FEAT_GUARDED_STORAGE) && len >= ADTL_GS_MIN_SIZE) {
2742cca53fdSDavid Hildenbrand         for (i = 0; i < 4; i++) {
2752cca53fdSDavid Hildenbrand             sa->gscb[i] = cpu_to_be64(cpu->env.gscb[i]);
2762cca53fdSDavid Hildenbrand         }
277f875cb0cSDavid Hildenbrand     }
278f875cb0cSDavid Hildenbrand 
2792cca53fdSDavid Hildenbrand     cpu_physical_memory_unmap(sa, len, 1, len);
280f875cb0cSDavid Hildenbrand     return 0;
281f875cb0cSDavid Hildenbrand }
282