xref: /qemu/accel/tcg/user-exec.c (revision 76eb88b1)
1 /*
2  *  User emulator execution
3  *
4  *  Copyright (c) 2003-2005 Fabrice Bellard
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.1 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 #include "qemu/osdep.h"
20 #include "hw/core/tcg-cpu-ops.h"
21 #include "disas/disas.h"
22 #include "exec/exec-all.h"
23 #include "tcg/tcg.h"
24 #include "qemu/bitops.h"
25 #include "exec/cpu_ldst.h"
26 #include "exec/translate-all.h"
27 #include "exec/helper-proto.h"
28 #include "qemu/atomic128.h"
29 #include "trace/trace-root.h"
30 #include "tcg/tcg-ldst.h"
31 #include "internal.h"
32 
33 __thread uintptr_t helper_retaddr;
34 
35 //#define DEBUG_SIGNAL
36 
37 /*
38  * Adjust the pc to pass to cpu_restore_state; return the memop type.
39  */
40 MMUAccessType adjust_signal_pc(uintptr_t *pc, bool is_write)
41 {
42     switch (helper_retaddr) {
43     default:
44         /*
45          * Fault during host memory operation within a helper function.
46          * The helper's host return address, saved here, gives us a
47          * pointer into the generated code that will unwind to the
48          * correct guest pc.
49          */
50         *pc = helper_retaddr;
51         break;
52 
53     case 0:
54         /*
55          * Fault during host memory operation within generated code.
56          * (Or, a unrelated bug within qemu, but we can't tell from here).
57          *
58          * We take the host pc from the signal frame.  However, we cannot
59          * use that value directly.  Within cpu_restore_state_from_tb, we
60          * assume PC comes from GETPC(), as used by the helper functions,
61          * so we adjust the address by -GETPC_ADJ to form an address that
62          * is within the call insn, so that the address does not accidentally
63          * match the beginning of the next guest insn.  However, when the
64          * pc comes from the signal frame it points to the actual faulting
65          * host memory insn and not the return from a call insn.
66          *
67          * Therefore, adjust to compensate for what will be done later
68          * by cpu_restore_state_from_tb.
69          */
70         *pc += GETPC_ADJ;
71         break;
72 
73     case 1:
74         /*
75          * Fault during host read for translation, or loosely, "execution".
76          *
77          * The guest pc is already pointing to the start of the TB for which
78          * code is being generated.  If the guest translator manages the
79          * page crossings correctly, this is exactly the correct address
80          * (and if the translator doesn't handle page boundaries correctly
81          * there's little we can do about that here).  Therefore, do not
82          * trigger the unwinder.
83          */
84         *pc = 0;
85         return MMU_INST_FETCH;
86     }
87 
88     return is_write ? MMU_DATA_STORE : MMU_DATA_LOAD;
89 }
90 
91 /**
92  * handle_sigsegv_accerr_write:
93  * @cpu: the cpu context
94  * @old_set: the sigset_t from the signal ucontext_t
95  * @host_pc: the host pc, adjusted for the signal
96  * @guest_addr: the guest address of the fault
97  *
98  * Return true if the write fault has been handled, and should be re-tried.
99  *
100  * Note that it is important that we don't call page_unprotect() unless
101  * this is really a "write to nonwritable page" fault, because
102  * page_unprotect() assumes that if it is called for an access to
103  * a page that's writable this means we had two threads racing and
104  * another thread got there first and already made the page writable;
105  * so we will retry the access. If we were to call page_unprotect()
106  * for some other kind of fault that should really be passed to the
107  * guest, we'd end up in an infinite loop of retrying the faulting access.
108  */
109 bool handle_sigsegv_accerr_write(CPUState *cpu, sigset_t *old_set,
110                                  uintptr_t host_pc, abi_ptr guest_addr)
111 {
112     switch (page_unprotect(guest_addr, host_pc)) {
113     case 0:
114         /*
115          * Fault not caused by a page marked unwritable to protect
116          * cached translations, must be the guest binary's problem.
117          */
118         return false;
119     case 1:
120         /*
121          * Fault caused by protection of cached translation; TBs
122          * invalidated, so resume execution.
123          */
124         return true;
125     case 2:
126         /*
127          * Fault caused by protection of cached translation, and the
128          * currently executing TB was modified and must be exited immediately.
129          */
130         sigprocmask(SIG_SETMASK, old_set, NULL);
131         cpu_loop_exit_noexc(cpu);
132         /* NORETURN */
133     default:
134         g_assert_not_reached();
135     }
136 }
137 
138 static int probe_access_internal(CPUArchState *env, target_ulong addr,
139                                  int fault_size, MMUAccessType access_type,
140                                  bool nonfault, uintptr_t ra)
141 {
142     int acc_flag;
143     bool maperr;
144 
145     switch (access_type) {
146     case MMU_DATA_STORE:
147         acc_flag = PAGE_WRITE_ORG;
148         break;
149     case MMU_DATA_LOAD:
150         acc_flag = PAGE_READ;
151         break;
152     case MMU_INST_FETCH:
153         acc_flag = PAGE_EXEC;
154         break;
155     default:
156         g_assert_not_reached();
157     }
158 
159     if (guest_addr_valid_untagged(addr)) {
160         int page_flags = page_get_flags(addr);
161         if (page_flags & acc_flag) {
162             return 0; /* success */
163         }
164         maperr = !(page_flags & PAGE_VALID);
165     } else {
166         maperr = true;
167     }
168 
169     if (nonfault) {
170         return TLB_INVALID_MASK;
171     }
172 
173     cpu_loop_exit_sigsegv(env_cpu(env), addr, access_type, maperr, ra);
174 }
175 
176 int probe_access_flags(CPUArchState *env, target_ulong addr,
177                        MMUAccessType access_type, int mmu_idx,
178                        bool nonfault, void **phost, uintptr_t ra)
179 {
180     int flags;
181 
182     flags = probe_access_internal(env, addr, 0, access_type, nonfault, ra);
183     *phost = flags ? NULL : g2h(env_cpu(env), addr);
184     return flags;
185 }
186 
187 void *probe_access(CPUArchState *env, target_ulong addr, int size,
188                    MMUAccessType access_type, int mmu_idx, uintptr_t ra)
189 {
190     int flags;
191 
192     g_assert(-(addr | TARGET_PAGE_MASK) >= size);
193     flags = probe_access_internal(env, addr, size, access_type, false, ra);
194     g_assert(flags == 0);
195 
196     return size ? g2h(env_cpu(env), addr) : NULL;
197 }
198 
199 tb_page_addr_t get_page_addr_code_hostp(CPUArchState *env, target_ulong addr,
200                                         void **hostp)
201 {
202     int flags;
203 
204     flags = probe_access_internal(env, addr, 1, MMU_INST_FETCH, false, 0);
205     g_assert(flags == 0);
206 
207     if (hostp) {
208         *hostp = g2h_untagged(addr);
209     }
210     return addr;
211 }
212 
213 /* The softmmu versions of these helpers are in cputlb.c.  */
214 
215 /*
216  * Verify that we have passed the correct MemOp to the correct function.
217  *
218  * We could present one function to target code, and dispatch based on
219  * the MemOp, but so far we have worked hard to avoid an indirect function
220  * call along the memory path.
221  */
222 static void validate_memop(MemOpIdx oi, MemOp expected)
223 {
224 #ifdef CONFIG_DEBUG_TCG
225     MemOp have = get_memop(oi) & (MO_SIZE | MO_BSWAP);
226     assert(have == expected);
227 #endif
228 }
229 
230 void helper_unaligned_ld(CPUArchState *env, target_ulong addr)
231 {
232     cpu_loop_exit_sigbus(env_cpu(env), addr, MMU_DATA_LOAD, GETPC());
233 }
234 
235 void helper_unaligned_st(CPUArchState *env, target_ulong addr)
236 {
237     cpu_loop_exit_sigbus(env_cpu(env), addr, MMU_DATA_STORE, GETPC());
238 }
239 
240 static void *cpu_mmu_lookup(CPUArchState *env, target_ulong addr,
241                             MemOpIdx oi, uintptr_t ra, MMUAccessType type)
242 {
243     MemOp mop = get_memop(oi);
244     int a_bits = get_alignment_bits(mop);
245     void *ret;
246 
247     /* Enforce guest required alignment.  */
248     if (unlikely(addr & ((1 << a_bits) - 1))) {
249         cpu_loop_exit_sigbus(env_cpu(env), addr, type, ra);
250     }
251 
252     ret = g2h(env_cpu(env), addr);
253     set_helper_retaddr(ra);
254     return ret;
255 }
256 
257 uint8_t cpu_ldb_mmu(CPUArchState *env, abi_ptr addr,
258                     MemOpIdx oi, uintptr_t ra)
259 {
260     void *haddr;
261     uint8_t ret;
262 
263     validate_memop(oi, MO_UB);
264     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD);
265     ret = ldub_p(haddr);
266     clear_helper_retaddr();
267     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R);
268     return ret;
269 }
270 
271 uint16_t cpu_ldw_be_mmu(CPUArchState *env, abi_ptr addr,
272                         MemOpIdx oi, uintptr_t ra)
273 {
274     void *haddr;
275     uint16_t ret;
276 
277     validate_memop(oi, MO_BEUW);
278     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD);
279     ret = lduw_be_p(haddr);
280     clear_helper_retaddr();
281     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R);
282     return ret;
283 }
284 
285 uint32_t cpu_ldl_be_mmu(CPUArchState *env, abi_ptr addr,
286                         MemOpIdx oi, uintptr_t ra)
287 {
288     void *haddr;
289     uint32_t ret;
290 
291     validate_memop(oi, MO_BEUL);
292     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD);
293     ret = ldl_be_p(haddr);
294     clear_helper_retaddr();
295     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R);
296     return ret;
297 }
298 
299 uint64_t cpu_ldq_be_mmu(CPUArchState *env, abi_ptr addr,
300                         MemOpIdx oi, uintptr_t ra)
301 {
302     void *haddr;
303     uint64_t ret;
304 
305     validate_memop(oi, MO_BEUQ);
306     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD);
307     ret = ldq_be_p(haddr);
308     clear_helper_retaddr();
309     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R);
310     return ret;
311 }
312 
313 uint16_t cpu_ldw_le_mmu(CPUArchState *env, abi_ptr addr,
314                         MemOpIdx oi, uintptr_t ra)
315 {
316     void *haddr;
317     uint16_t ret;
318 
319     validate_memop(oi, MO_LEUW);
320     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD);
321     ret = lduw_le_p(haddr);
322     clear_helper_retaddr();
323     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R);
324     return ret;
325 }
326 
327 uint32_t cpu_ldl_le_mmu(CPUArchState *env, abi_ptr addr,
328                         MemOpIdx oi, uintptr_t ra)
329 {
330     void *haddr;
331     uint32_t ret;
332 
333     validate_memop(oi, MO_LEUL);
334     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD);
335     ret = ldl_le_p(haddr);
336     clear_helper_retaddr();
337     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R);
338     return ret;
339 }
340 
341 uint64_t cpu_ldq_le_mmu(CPUArchState *env, abi_ptr addr,
342                         MemOpIdx oi, uintptr_t ra)
343 {
344     void *haddr;
345     uint64_t ret;
346 
347     validate_memop(oi, MO_LEUQ);
348     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_LOAD);
349     ret = ldq_le_p(haddr);
350     clear_helper_retaddr();
351     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_R);
352     return ret;
353 }
354 
355 void cpu_stb_mmu(CPUArchState *env, abi_ptr addr, uint8_t val,
356                  MemOpIdx oi, uintptr_t ra)
357 {
358     void *haddr;
359 
360     validate_memop(oi, MO_UB);
361     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE);
362     stb_p(haddr, val);
363     clear_helper_retaddr();
364     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W);
365 }
366 
367 void cpu_stw_be_mmu(CPUArchState *env, abi_ptr addr, uint16_t val,
368                     MemOpIdx oi, uintptr_t ra)
369 {
370     void *haddr;
371 
372     validate_memop(oi, MO_BEUW);
373     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE);
374     stw_be_p(haddr, val);
375     clear_helper_retaddr();
376     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W);
377 }
378 
379 void cpu_stl_be_mmu(CPUArchState *env, abi_ptr addr, uint32_t val,
380                     MemOpIdx oi, uintptr_t ra)
381 {
382     void *haddr;
383 
384     validate_memop(oi, MO_BEUL);
385     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE);
386     stl_be_p(haddr, val);
387     clear_helper_retaddr();
388     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W);
389 }
390 
391 void cpu_stq_be_mmu(CPUArchState *env, abi_ptr addr, uint64_t val,
392                     MemOpIdx oi, uintptr_t ra)
393 {
394     void *haddr;
395 
396     validate_memop(oi, MO_BEUQ);
397     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE);
398     stq_be_p(haddr, val);
399     clear_helper_retaddr();
400     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W);
401 }
402 
403 void cpu_stw_le_mmu(CPUArchState *env, abi_ptr addr, uint16_t val,
404                     MemOpIdx oi, uintptr_t ra)
405 {
406     void *haddr;
407 
408     validate_memop(oi, MO_LEUW);
409     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE);
410     stw_le_p(haddr, val);
411     clear_helper_retaddr();
412     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W);
413 }
414 
415 void cpu_stl_le_mmu(CPUArchState *env, abi_ptr addr, uint32_t val,
416                     MemOpIdx oi, uintptr_t ra)
417 {
418     void *haddr;
419 
420     validate_memop(oi, MO_LEUL);
421     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE);
422     stl_le_p(haddr, val);
423     clear_helper_retaddr();
424     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W);
425 }
426 
427 void cpu_stq_le_mmu(CPUArchState *env, abi_ptr addr, uint64_t val,
428                     MemOpIdx oi, uintptr_t ra)
429 {
430     void *haddr;
431 
432     validate_memop(oi, MO_LEUQ);
433     haddr = cpu_mmu_lookup(env, addr, oi, ra, MMU_DATA_STORE);
434     stq_le_p(haddr, val);
435     clear_helper_retaddr();
436     qemu_plugin_vcpu_mem_cb(env_cpu(env), addr, oi, QEMU_PLUGIN_MEM_W);
437 }
438 
439 uint32_t cpu_ldub_code(CPUArchState *env, abi_ptr ptr)
440 {
441     uint32_t ret;
442 
443     set_helper_retaddr(1);
444     ret = ldub_p(g2h_untagged(ptr));
445     clear_helper_retaddr();
446     return ret;
447 }
448 
449 uint32_t cpu_lduw_code(CPUArchState *env, abi_ptr ptr)
450 {
451     uint32_t ret;
452 
453     set_helper_retaddr(1);
454     ret = lduw_p(g2h_untagged(ptr));
455     clear_helper_retaddr();
456     return ret;
457 }
458 
459 uint32_t cpu_ldl_code(CPUArchState *env, abi_ptr ptr)
460 {
461     uint32_t ret;
462 
463     set_helper_retaddr(1);
464     ret = ldl_p(g2h_untagged(ptr));
465     clear_helper_retaddr();
466     return ret;
467 }
468 
469 uint64_t cpu_ldq_code(CPUArchState *env, abi_ptr ptr)
470 {
471     uint64_t ret;
472 
473     set_helper_retaddr(1);
474     ret = ldq_p(g2h_untagged(ptr));
475     clear_helper_retaddr();
476     return ret;
477 }
478 
479 #include "ldst_common.c.inc"
480 
481 /*
482  * Do not allow unaligned operations to proceed.  Return the host address.
483  *
484  * @prot may be PAGE_READ, PAGE_WRITE, or PAGE_READ|PAGE_WRITE.
485  */
486 static void *atomic_mmu_lookup(CPUArchState *env, target_ulong addr,
487                                MemOpIdx oi, int size, int prot,
488                                uintptr_t retaddr)
489 {
490     MemOp mop = get_memop(oi);
491     int a_bits = get_alignment_bits(mop);
492     void *ret;
493 
494     /* Enforce guest required alignment.  */
495     if (unlikely(addr & ((1 << a_bits) - 1))) {
496         MMUAccessType t = prot == PAGE_READ ? MMU_DATA_LOAD : MMU_DATA_STORE;
497         cpu_loop_exit_sigbus(env_cpu(env), addr, t, retaddr);
498     }
499 
500     /* Enforce qemu required alignment.  */
501     if (unlikely(addr & (size - 1))) {
502         cpu_loop_exit_atomic(env_cpu(env), retaddr);
503     }
504 
505     ret = g2h(env_cpu(env), addr);
506     set_helper_retaddr(retaddr);
507     return ret;
508 }
509 
510 #include "atomic_common.c.inc"
511 
512 /*
513  * First set of functions passes in OI and RETADDR.
514  * This makes them callable from other helpers.
515  */
516 
517 #define ATOMIC_NAME(X) \
518     glue(glue(glue(cpu_atomic_ ## X, SUFFIX), END), _mmu)
519 #define ATOMIC_MMU_CLEANUP do { clear_helper_retaddr(); } while (0)
520 
521 #define DATA_SIZE 1
522 #include "atomic_template.h"
523 
524 #define DATA_SIZE 2
525 #include "atomic_template.h"
526 
527 #define DATA_SIZE 4
528 #include "atomic_template.h"
529 
530 #ifdef CONFIG_ATOMIC64
531 #define DATA_SIZE 8
532 #include "atomic_template.h"
533 #endif
534 
535 #if HAVE_ATOMIC128 || HAVE_CMPXCHG128
536 #define DATA_SIZE 16
537 #include "atomic_template.h"
538 #endif
539