451ab91e |
1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
2 | * Mupen64plus - rjump.c * |
3 | * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ * |
4 | * Copyright (C) 2007 Richard Goedeken (Richard42) * |
5 | * Copyright (C) 2002 Hacktarux * |
6 | * * |
7 | * This program is free software; you can redistribute it and/or modify * |
8 | * it under the terms of the GNU General Public License as published by * |
9 | * the Free Software Foundation; either version 2 of the License, or * |
10 | * (at your option) any later version. * |
11 | * * |
12 | * This program is distributed in the hope that it will be useful, * |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
15 | * GNU General Public License for more details. * |
16 | * * |
17 | * You should have received a copy of the GNU General Public License * |
18 | * along with this program; if not, write to the * |
19 | * Free Software Foundation, Inc., * |
20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
21 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
22 | |
23 | #include <stdlib.h> |
24 | |
25 | #include "api/m64p_types.h" |
26 | #include "api/callbacks.h" |
27 | #include "r4300/recomp.h" |
28 | #include "r4300/r4300.h" |
29 | #include "r4300/macros.h" |
30 | #include "r4300/ops.h" |
31 | #include "r4300/recomph.h" |
32 | |
33 | // that's where the dynarec will restart when going back from a C function |
34 | static unsigned long long *return_address; |
35 | |
36 | void dyna_jump(void) |
37 | { |
38 | if (stop == 1) |
39 | { |
40 | dyna_stop(); |
41 | return; |
42 | } |
43 | |
44 | if (PC->reg_cache_infos.need_map) |
45 | *return_address = (unsigned long long) (PC->reg_cache_infos.jump_wrapper); |
46 | else |
47 | *return_address = (unsigned long long) (actual->code + PC->local_addr); |
48 | } |
49 | |
50 | static long long save_rsp = 0; |
51 | static long long save_rip = 0; |
52 | |
53 | void dyna_start(void *code) |
54 | { |
55 | /* save the base and stack pointers */ |
56 | /* make a call and a pop to retrieve the instruction pointer and save it too */ |
57 | /* then call the code(), which should theoretically never return. */ |
58 | /* When dyna_stop() sets the *return_address to the saved RIP, the emulator thread will come back here. */ |
59 | /* It will jump to label 2, restore the base and stack pointers, and exit this function */ |
60 | DebugMessage(M64MSG_INFO, "R4300: starting 64-bit dynamic recompiler at: %p", code); |
61 | #if defined(__GNUC__) && defined(__x86_64__) |
62 | asm volatile |
63 | (" push %%rbx \n" /* we must push an even # of registers to keep stack 16-byte aligned */ |
64 | " push %%r12 \n" |
65 | " push %%r13 \n" |
66 | " push %%r14 \n" |
67 | " push %%r15 \n" |
68 | " push %%rbp \n" |
69 | " mov %%rsp, %[save_rsp] \n" |
70 | " lea %[reg], %%r15 \n" /* store the base location of the r4300 registers in r15 for addressing */ |
71 | " call 1f \n" |
72 | " jmp 2f \n" |
73 | "1: \n" |
74 | " pop %%rax \n" |
75 | " mov %%rax, %[save_rip] \n" |
76 | |
77 | " sub $0x10, %%rsp \n" |
78 | " and $-16, %%rsp \n" /* ensure that stack is 16-byte aligned */ |
79 | " mov %%rsp, %%rax \n" |
80 | " sub $8, %%rax \n" |
81 | " mov %%rax, %[return_address]\n" |
82 | |
83 | " call *%%rbx \n" |
84 | "2: \n" |
85 | " mov %[save_rsp], %%rsp \n" |
86 | " pop %%rbp \n" |
87 | " pop %%r15 \n" |
88 | " pop %%r14 \n" |
89 | " pop %%r13 \n" |
90 | " pop %%r12 \n" |
91 | " pop %%rbx \n" |
92 | : [save_rsp]"=m"(save_rsp), [save_rip]"=m"(save_rip), [return_address]"=m"(return_address) |
93 | : "b" (code), [reg]"m"(*reg) |
94 | : "%rax", "memory" |
95 | ); |
96 | #endif |
97 | |
98 | /* clear the registers so we don't return here a second time; that would be a bug */ |
99 | save_rsp=0; |
100 | save_rip=0; |
101 | } |
102 | |
103 | void dyna_stop(void) |
104 | { |
105 | if (save_rip == 0) |
106 | DebugMessage(M64MSG_WARNING, "Instruction pointer is 0 at dyna_stop()"); |
107 | else |
108 | { |
109 | *return_address = (unsigned long long) save_rip; |
110 | } |
111 | } |
112 | |