Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004 Jeff Dike (jdike@addtoit.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #ifndef __SYSDEP_STUB_H |
| 7 | #define __SYSDEP_STUB_H |
| 8 | |
Jeff Dike | 5b7b15a | 2005-12-18 17:50:39 +0100 | [diff] [blame] | 9 | #include <sys/mman.h> |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 10 | #include <asm/unistd.h> |
| 11 | #include <sysdep/ptrace_user.h> |
Jeff Dike | 5b7b15a | 2005-12-18 17:50:39 +0100 | [diff] [blame] | 12 | #include "stub-data.h" |
| 13 | #include "kern_constants.h" |
| 14 | #include "uml-config.h" |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 15 | |
| 16 | extern void stub_segv_handler(int sig); |
Bodo Stroesser | 9786a8f | 2005-07-07 17:56:50 -0700 | [diff] [blame] | 17 | extern void stub_clone_handler(void); |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 18 | |
| 19 | #define STUB_SYSCALL_RET PT_INDEX(RAX) |
| 20 | #define STUB_MMAP_NR __NR_mmap |
| 21 | #define MMAP_OFFSET(o) (o) |
| 22 | |
Paolo 'Blaisorblade' Giarrusso | 4f02724 | 2005-11-07 00:58:46 -0800 | [diff] [blame] | 23 | #define __syscall_clobber "r11","rcx","memory" |
| 24 | #define __syscall "syscall" |
| 25 | |
Jeff Dike | 17d4697 | 2005-11-21 21:32:09 -0800 | [diff] [blame] | 26 | static inline long stub_syscall0(long syscall) |
| 27 | { |
| 28 | long ret; |
| 29 | |
| 30 | __asm__ volatile (__syscall |
| 31 | : "=a" (ret) |
| 32 | : "0" (syscall) : __syscall_clobber ); |
| 33 | |
| 34 | return ret; |
| 35 | } |
| 36 | |
Bodo Stroesser | 9786a8f | 2005-07-07 17:56:50 -0700 | [diff] [blame] | 37 | static inline long stub_syscall2(long syscall, long arg1, long arg2) |
| 38 | { |
| 39 | long ret; |
| 40 | |
Paolo 'Blaisorblade' Giarrusso | 4f02724 | 2005-11-07 00:58:46 -0800 | [diff] [blame] | 41 | __asm__ volatile (__syscall |
| 42 | : "=a" (ret) |
| 43 | : "0" (syscall), "D" (arg1), "S" (arg2) : __syscall_clobber ); |
| 44 | |
| 45 | return ret; |
Bodo Stroesser | 9786a8f | 2005-07-07 17:56:50 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static inline long stub_syscall3(long syscall, long arg1, long arg2, long arg3) |
| 49 | { |
Paolo 'Blaisorblade' Giarrusso | 4f02724 | 2005-11-07 00:58:46 -0800 | [diff] [blame] | 50 | long ret; |
| 51 | |
| 52 | __asm__ volatile (__syscall |
| 53 | : "=a" (ret) |
| 54 | : "0" (syscall), "D" (arg1), "S" (arg2), "d" (arg3) |
| 55 | : __syscall_clobber ); |
| 56 | |
| 57 | return ret; |
Bodo Stroesser | 9786a8f | 2005-07-07 17:56:50 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | static inline long stub_syscall4(long syscall, long arg1, long arg2, long arg3, |
| 61 | long arg4) |
| 62 | { |
Paolo 'Blaisorblade' Giarrusso | 4f02724 | 2005-11-07 00:58:46 -0800 | [diff] [blame] | 63 | long ret; |
| 64 | |
| 65 | __asm__ volatile ("movq %5,%%r10 ; " __syscall |
| 66 | : "=a" (ret) |
| 67 | : "0" (syscall), "D" (arg1), "S" (arg2), "d" (arg3), |
| 68 | "g" (arg4) |
| 69 | : __syscall_clobber, "r10" ); |
| 70 | |
| 71 | return ret; |
| 72 | } |
| 73 | |
| 74 | static inline long stub_syscall5(long syscall, long arg1, long arg2, long arg3, |
| 75 | long arg4, long arg5) |
| 76 | { |
| 77 | long ret; |
| 78 | |
| 79 | __asm__ volatile ("movq %5,%%r10 ; movq %6,%%r8 ; " __syscall |
| 80 | : "=a" (ret) |
| 81 | : "0" (syscall), "D" (arg1), "S" (arg2), "d" (arg3), |
| 82 | "g" (arg4), "g" (arg5) |
| 83 | : __syscall_clobber, "r10", "r8" ); |
| 84 | |
| 85 | return ret; |
Bodo Stroesser | 9786a8f | 2005-07-07 17:56:50 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Bodo Stroesser | 9786a8f | 2005-07-07 17:56:50 -0700 | [diff] [blame] | 88 | static inline void trap_myself(void) |
| 89 | { |
| 90 | __asm("int3"); |
| 91 | } |
| 92 | |
Jeff Dike | 5b7b15a | 2005-12-18 17:50:39 +0100 | [diff] [blame] | 93 | static inline void remap_stack(long fd, unsigned long offset) |
| 94 | { |
| 95 | __asm__ volatile ("movq %4,%%r10 ; movq %5,%%r8 ; " |
| 96 | "movq %6, %%r9; " __syscall "; movq %7, %%rbx ; " |
| 97 | "movq %%rax, (%%rbx)": |
| 98 | : "a" (STUB_MMAP_NR), "D" (UML_CONFIG_STUB_DATA), |
| 99 | "S" (UM_KERN_PAGE_SIZE), |
| 100 | "d" (PROT_READ | PROT_WRITE), |
| 101 | "g" (MAP_FIXED | MAP_SHARED), "g" (fd), |
| 102 | "g" (offset), |
| 103 | "i" (&((struct stub_data *) UML_CONFIG_STUB_DATA)->err) |
| 104 | : __syscall_clobber, "r10", "r8", "r9" ); |
| 105 | } |
| 106 | |
Jeff Dike | d67b569 | 2005-07-07 17:56:49 -0700 | [diff] [blame] | 107 | #endif |