xref: /openbmc/qemu/target/s390x/diag.c (revision 2f15b79280cf71b7991dfd3f0312a1797630e376)
1 /*
2  * S390x DIAG instruction helper functions
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14 
15 #include "qemu/osdep.h"
16 #include "cpu.h"
17 #include "internal.h"
18 #include "exec/address-spaces.h"
19 #include "exec/exec-all.h"
20 #include "hw/watchdog/wdt_diag288.h"
21 #include "sysemu/cpus.h"
22 #include "hw/s390x/ipl.h"
23 #include "hw/s390x/s390-virtio-ccw.h"
24 
25 int handle_diag_288(CPUS390XState *env, uint64_t r1, uint64_t r3)
26 {
27     uint64_t func = env->regs[r1];
28     uint64_t timeout = env->regs[r1 + 1];
29     uint64_t action = env->regs[r3];
30     Object *obj;
31     DIAG288State *diag288;
32     DIAG288Class *diag288_class;
33 
34     if (r1 % 2 || action != 0) {
35         return -1;
36     }
37 
38     /* Timeout must be more than 15 seconds except for timer deletion */
39     if (func != WDT_DIAG288_CANCEL && timeout < 15) {
40         return -1;
41     }
42 
43     obj = object_resolve_path_type("", TYPE_WDT_DIAG288, NULL);
44     if (!obj) {
45         return -1;
46     }
47 
48     diag288 = DIAG288(obj);
49     diag288_class = DIAG288_GET_CLASS(diag288);
50     return diag288_class->handle_timer(diag288, func, timeout);
51 }
52 
53 #define DIAG_308_RC_OK              0x0001
54 #define DIAG_308_RC_NO_CONF         0x0102
55 #define DIAG_308_RC_INVALID         0x0402
56 
57 void handle_diag_308(CPUS390XState *env, uint64_t r1, uint64_t r3, uintptr_t ra)
58 {
59     CPUState *cs = CPU(s390_env_get_cpu(env));
60     uint64_t addr =  env->regs[r1];
61     uint64_t subcode = env->regs[r3];
62     IplParameterBlock *iplb;
63 
64     if (env->psw.mask & PSW_MASK_PSTATE) {
65         s390_program_interrupt(env, PGM_PRIVILEGED, ILEN_AUTO, ra);
66         return;
67     }
68 
69     if ((subcode & ~0x0ffffULL) || (subcode > 6)) {
70         s390_program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO, ra);
71         return;
72     }
73 
74     switch (subcode) {
75     case 0:
76         s390_ipl_reset_request(cs, S390_RESET_MODIFIED_CLEAR);
77         break;
78     case 1:
79         s390_ipl_reset_request(cs, S390_RESET_LOAD_NORMAL);
80         break;
81     case 3:
82         s390_ipl_reset_request(cs, S390_RESET_REIPL);
83         break;
84     case 5:
85         if ((r1 & 1) || (addr & 0x0fffULL)) {
86             s390_program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO, ra);
87             return;
88         }
89         if (!address_space_access_valid(&address_space_memory, addr,
90                                         sizeof(IplParameterBlock), false,
91                                         MEMTXATTRS_UNSPECIFIED)) {
92             s390_program_interrupt(env, PGM_ADDRESSING, ILEN_AUTO, ra);
93             return;
94         }
95         iplb = g_new0(IplParameterBlock, 1);
96         cpu_physical_memory_read(addr, iplb, sizeof(iplb->len));
97         if (!iplb_valid_len(iplb)) {
98             env->regs[r1 + 1] = DIAG_308_RC_INVALID;
99             goto out;
100         }
101 
102         cpu_physical_memory_read(addr, iplb, be32_to_cpu(iplb->len));
103 
104         if (!iplb_valid_ccw(iplb) && !iplb_valid_fcp(iplb)) {
105             env->regs[r1 + 1] = DIAG_308_RC_INVALID;
106             goto out;
107         }
108 
109         s390_ipl_update_diag308(iplb);
110         env->regs[r1 + 1] = DIAG_308_RC_OK;
111 out:
112         g_free(iplb);
113         return;
114     case 6:
115         if ((r1 & 1) || (addr & 0x0fffULL)) {
116             s390_program_interrupt(env, PGM_SPECIFICATION, ILEN_AUTO, ra);
117             return;
118         }
119         if (!address_space_access_valid(&address_space_memory, addr,
120                                         sizeof(IplParameterBlock), true,
121                                         MEMTXATTRS_UNSPECIFIED)) {
122             s390_program_interrupt(env, PGM_ADDRESSING, ILEN_AUTO, ra);
123             return;
124         }
125         iplb = s390_ipl_get_iplb();
126         if (iplb) {
127             cpu_physical_memory_write(addr, iplb, be32_to_cpu(iplb->len));
128             env->regs[r1 + 1] = DIAG_308_RC_OK;
129         } else {
130             env->regs[r1 + 1] = DIAG_308_RC_NO_CONF;
131         }
132         return;
133     default:
134         hw_error("Unhandled diag308 subcode %" PRIx64, subcode);
135         break;
136     }
137 }
138