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 *
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. *
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. *
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 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
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"
33 // that's where the dynarec will restart when going back from a C function
34 static unsigned long long *return_address;
44 if (PC->reg_cache_infos.need_map)
45 *return_address = (unsigned long long) (PC->reg_cache_infos.jump_wrapper);
47 *return_address = (unsigned long long) (actual->code + PC->local_addr);
50 static long long save_rsp = 0;
51 static long long save_rip = 0;
53 void dyna_start(void *code)
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__)
63 (" push %%rbx \n" /* we must push an even # of registers to keep stack 16-byte aligned */
69 " mov %%rsp, %[save_rsp] \n"
70 " lea %[reg], %%r15 \n" /* store the base location of the r4300 registers in r15 for addressing */
75 " mov %%rax, %[save_rip] \n"
77 " sub $0x10, %%rsp \n"
78 " and $-16, %%rsp \n" /* ensure that stack is 16-byte aligned */
79 " mov %%rsp, %%rax \n"
81 " mov %%rax, %[return_address]\n"
85 " mov %[save_rsp], %%rsp \n"
92 : [save_rsp]"=m"(save_rsp), [save_rip]"=m"(save_rip), [return_address]"=m"(return_address)
93 : "b" (code), [reg]"m"(*reg)
98 /* clear the registers so we don't return here a second time; that would be a bug */
106 DebugMessage(M64MSG_WARNING, "Instruction pointer is 0 at dyna_stop()");
109 *return_address = (unsigned long long) save_rip;