aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Pall <mike>2011-12-12 22:49:19 +0100
committerMike Pall <mike>2011-12-12 23:10:46 +0100
commit10474987bd58a2183d848cc7ef40aa2d4e9125ba (patch)
treedce63a72e48b85750e06430cc838f6e7ba3d344b
parent2d8f3d75dfae1839224cf0c726eef9cb3b77eaa3 (diff)
downloadluajit-10474987bd58a2183d848cc7ef40aa2d4e9125ba.tar.gz
luajit-10474987bd58a2183d848cc7ef40aa2d4e9125ba.tar.bz2
luajit-10474987bd58a2183d848cc7ef40aa2d4e9125ba.zip
Move helper for syncing data/instruction cache to lj_mcode.c.
Sync caches after dynamic code generation for FFI callbacks.
-rw-r--r--src/Makefile.dep10
-rw-r--r--src/lj_asm.c45
-rw-r--r--src/lj_asm_arm.h2
-rw-r--r--src/lj_asm_ppc.h4
-rw-r--r--src/lj_asm_x86.h2
-rw-r--r--src/lj_ccallback.c2
-rw-r--r--src/lj_mcode.c51
-rw-r--r--src/lj_mcode.h9
8 files changed, 69 insertions, 56 deletions
diff --git a/src/Makefile.dep b/src/Makefile.dep
index afcf5c12..add7508a 100644
--- a/src/Makefile.dep
+++ b/src/Makefile.dep
@@ -75,7 +75,7 @@ lj_ccall.o: lj_ccall.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
75lj_ccallback.o: lj_ccallback.c lj_obj.h lua.h luaconf.h lj_def.h \ 75lj_ccallback.o: lj_ccallback.c lj_obj.h lua.h luaconf.h lj_def.h \
76 lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_state.h lj_frame.h \ 76 lj_arch.h lj_gc.h lj_err.h lj_errmsg.h lj_tab.h lj_state.h lj_frame.h \
77 lj_bc.h lj_ctype.h lj_cconv.h lj_ccall.h lj_ccallback.h lj_target.h \ 77 lj_bc.h lj_ctype.h lj_cconv.h lj_ccall.h lj_ccallback.h lj_target.h \
78 lj_target_*.h lj_vm.h 78 lj_target_*.h lj_mcode.h lj_jit.h lj_ir.h lj_vm.h
79lj_cconv.o: lj_cconv.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \ 79lj_cconv.o: lj_cconv.c lj_obj.h lua.h luaconf.h lj_def.h lj_arch.h \
80 lj_err.h lj_errmsg.h lj_tab.h lj_ctype.h lj_gc.h lj_cdata.h lj_cconv.h \ 80 lj_err.h lj_errmsg.h lj_tab.h lj_ctype.h lj_gc.h lj_cdata.h lj_cconv.h \
81 lj_ccallback.h 81 lj_ccallback.h
@@ -195,10 +195,10 @@ ljamalg.o: ljamalg.c lua.h luaconf.h lauxlib.h lj_gc.c lj_obj.h lj_def.h \
195 lj_vmevent.h lj_vmmath.c lj_api.c lj_bcdump.h lj_parse.h lj_lex.c \ 195 lj_vmevent.h lj_vmmath.c lj_api.c lj_bcdump.h lj_parse.h lj_lex.c \
196 lualib.h lj_parse.c lj_bcread.c lj_bcwrite.c lj_ctype.c lj_ccallback.h \ 196 lualib.h lj_parse.c lj_bcread.c lj_bcwrite.c lj_ctype.c lj_ccallback.h \
197 lj_cdata.c lj_cconv.h lj_cconv.c lj_ccall.c lj_ccall.h lj_ccallback.c \ 197 lj_cdata.c lj_cconv.h lj_cconv.c lj_ccall.c lj_ccall.h lj_ccallback.c \
198 lj_target.h lj_target_*.h lj_carith.c lj_carith.h lj_clib.c lj_clib.h \ 198 lj_target.h lj_target_*.h lj_mcode.h lj_carith.c lj_carith.h lj_clib.c \
199 lj_cparse.c lj_cparse.h lj_lib.c lj_lib.h lj_ir.c lj_ircall.h lj_iropt.h \ 199 lj_clib.h lj_cparse.c lj_cparse.h lj_lib.c lj_lib.h lj_ir.c lj_ircall.h \
200 lj_opt_mem.c lj_opt_fold.c lj_folddef.h lj_opt_narrow.c lj_opt_dce.c \ 200 lj_iropt.h lj_opt_mem.c lj_opt_fold.c lj_folddef.h lj_opt_narrow.c \
201 lj_opt_loop.c lj_snap.h lj_opt_split.c lj_mcode.c lj_mcode.h lj_snap.c \ 201 lj_opt_dce.c lj_opt_loop.c lj_snap.h lj_opt_split.c lj_mcode.c lj_snap.c \
202 lj_record.c lj_record.h lj_ffrecord.h lj_crecord.c lj_crecord.h \ 202 lj_record.c lj_record.h lj_ffrecord.h lj_crecord.c lj_crecord.h \
203 lj_ffrecord.c lj_recdef.h lj_asm.c lj_asm.h lj_emit_*.h lj_asm_*.h \ 203 lj_ffrecord.c lj_recdef.h lj_asm.c lj_asm.h lj_emit_*.h lj_asm_*.h \
204 lj_trace.c lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c lib_base.c \ 204 lj_trace.c lj_gdbjit.h lj_gdbjit.c lj_alloc.c lib_aux.c lib_base.c \
diff --git a/src/lj_asm.c b/src/lj_asm.c
index 16671a8a..1103e998 100644
--- a/src/lj_asm.c
+++ b/src/lj_asm.c
@@ -147,14 +147,6 @@ IRFLDEF(FLOFS)
147 0 147 0
148}; 148};
149 149
150/* Define this if you want to run LuaJIT with Valgrind. */
151#ifdef LUAJIT_USE_VALGRIND
152#include <valgrind/valgrind.h>
153#define VG_INVALIDATE(p, sz) VALGRIND_DISCARD_TRANSLATIONS(p, sz)
154#else
155#define VG_INVALIDATE(p, sz) ((void)0)
156#endif
157
158/* -- Target-specific instruction emitter --------------------------------- */ 150/* -- Target-specific instruction emitter --------------------------------- */
159 151
160#if LJ_TARGET_X86ORX64 152#if LJ_TARGET_X86ORX64
@@ -931,41 +923,6 @@ static uint32_t ir_khash(IRIns *ir)
931 return hashrot(lo, hi); 923 return hashrot(lo, hi);
932} 924}
933 925
934#if !LJ_TARGET_X86ORX64 && LJ_TARGET_OSX
935void sys_icache_invalidate(void *start, size_t len);
936#endif
937
938#if LJ_TARGET_LINUX && LJ_TARGET_PPC
939#include <dlfcn.h>
940static void (*asm_ppc_cache_flush)(MCode *start, MCode *end);
941static void asm_dummy_cache_flush(MCode *start, MCode *end)
942{
943 UNUSED(start); UNUSED(end);
944}
945#endif
946
947/* Flush instruction cache. */
948static void asm_cache_flush(MCode *start, MCode *end)
949{
950 VG_INVALIDATE(start, (char *)end-(char *)start);
951#if LJ_TARGET_X86ORX64
952 UNUSED(start); UNUSED(end);
953#elif LJ_TARGET_OSX
954 sys_icache_invalidate(start, end-start);
955#elif LJ_TARGET_LINUX && LJ_TARGET_PPC
956 if (!asm_ppc_cache_flush) {
957 void *vdso = dlopen("linux-vdso32.so.1", RTLD_LAZY);
958 if (!vdso || !(asm_ppc_cache_flush = dlsym(vdso, "__kernel_sync_dicache")))
959 asm_ppc_cache_flush = asm_dummy_cache_flush;
960 }
961 asm_ppc_cache_flush(start, end);
962#elif defined(__GNUC__) && !LJ_TARGET_PPC
963 __clear_cache(start, end);
964#else
965#error "Missing builtin to flush instruction cache"
966#endif
967}
968
969/* -- Allocations --------------------------------------------------------- */ 926/* -- Allocations --------------------------------------------------------- */
970 927
971static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args); 928static void asm_gencall(ASMState *as, const CCallInfo *ci, IRRef *args);
@@ -1776,7 +1733,7 @@ void lj_asm_trace(jit_State *J, GCtrace *T)
1776 if (!as->loopref) 1733 if (!as->loopref)
1777 asm_tail_fixup(as, T->link); /* Note: this may change as->mctop! */ 1734 asm_tail_fixup(as, T->link); /* Note: this may change as->mctop! */
1778 T->szmcode = (MSize)((char *)as->mctop - (char *)as->mcp); 1735 T->szmcode = (MSize)((char *)as->mctop - (char *)as->mcp);
1779 asm_cache_flush(T->mcode, origtop); 1736 lj_mcode_sync(T->mcode, origtop);
1780} 1737}
1781 1738
1782#undef IR 1739#undef IR
diff --git a/src/lj_asm_arm.h b/src/lj_asm_arm.h
index 24b49e69..087fc0fb 100644
--- a/src/lj_asm_arm.h
+++ b/src/lj_asm_arm.h
@@ -1779,7 +1779,7 @@ void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)
1779 } 1779 }
1780 } 1780 }
1781 lua_assert(cstart != NULL); 1781 lua_assert(cstart != NULL);
1782 asm_cache_flush(cstart, cend); 1782 lj_mcode_sync(cstart, cend);
1783 lj_mcode_patch(J, mcarea, 1); 1783 lj_mcode_patch(J, mcarea, 1);
1784} 1784}
1785 1785
diff --git a/src/lj_asm_ppc.h b/src/lj_asm_ppc.h
index c03d0102..490ad816 100644
--- a/src/lj_asm_ppc.h
+++ b/src/lj_asm_ppc.h
@@ -2130,13 +2130,13 @@ void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)
2130 *px = PPCI_B | ((uint32_t)delta & 0x03ffffffu); 2130 *px = PPCI_B | ((uint32_t)delta & 0x03ffffffu);
2131 } 2131 }
2132 if (!cstart) cstart = px; 2132 if (!cstart) cstart = px;
2133 asm_cache_flush(cstart, px+1); 2133 lj_mcode_sync(cstart, px+1);
2134 if (clearso) { /* Extend the current trace. Ugly workaround. */ 2134 if (clearso) { /* Extend the current trace. Ugly workaround. */
2135 MCode *pp = J->cur.mcode; 2135 MCode *pp = J->cur.mcode;
2136 J->cur.szmcode += sizeof(MCode); 2136 J->cur.szmcode += sizeof(MCode);
2137 *--pp = PPCI_MCRXR; /* Clear SO flag. */ 2137 *--pp = PPCI_MCRXR; /* Clear SO flag. */
2138 J->cur.mcode = pp; 2138 J->cur.mcode = pp;
2139 asm_cache_flush(pp, pp+1); 2139 lj_mcode_sync(pp, pp+1);
2140 } 2140 }
2141 lj_mcode_patch(J, mcarea, 1); 2141 lj_mcode_patch(J, mcarea, 1);
2142} 2142}
diff --git a/src/lj_asm_x86.h b/src/lj_asm_x86.h
index c1f0fc15..1170b663 100644
--- a/src/lj_asm_x86.h
+++ b/src/lj_asm_x86.h
@@ -2745,7 +2745,7 @@ void lj_asm_patchexit(jit_State *J, GCtrace *T, ExitNo exitno, MCode *target)
2745 p += 5; 2745 p += 5;
2746 } 2746 }
2747 } 2747 }
2748 lj_mcode_sync(T->mcode, T->mcode + T->szmcode);
2748 lj_mcode_patch(J, mcarea, 1); 2749 lj_mcode_patch(J, mcarea, 1);
2749 VG_INVALIDATE(T->mcode, T->szmcode);
2750} 2750}
2751 2751
diff --git a/src/lj_ccallback.c b/src/lj_ccallback.c
index f8d95d39..e104c484 100644
--- a/src/lj_ccallback.c
+++ b/src/lj_ccallback.c
@@ -17,6 +17,7 @@
17#include "lj_ccall.h" 17#include "lj_ccall.h"
18#include "lj_ccallback.h" 18#include "lj_ccallback.h"
19#include "lj_target.h" 19#include "lj_target.h"
20#include "lj_mcode.h"
20#include "lj_vm.h" 21#include "lj_vm.h"
21 22
22/* -- Target-specific handling of callback slots -------------------------- */ 23/* -- Target-specific handling of callback slots -------------------------- */
@@ -145,6 +146,7 @@ static void callback_mcode_new(CTState *cts)
145#endif 146#endif
146 cts->cb.mcode = p; 147 cts->cb.mcode = p;
147 callback_mcode_init(cts->g, p); 148 callback_mcode_init(cts->g, p);
149 lj_mcode_sync(p, (char *)p + sz);
148#if LJ_TARGET_WINDOWS 150#if LJ_TARGET_WINDOWS
149 { 151 {
150 DWORD oprot; 152 DWORD oprot;
diff --git a/src/lj_mcode.c b/src/lj_mcode.c
index 279854f8..90ac34fc 100644
--- a/src/lj_mcode.c
+++ b/src/lj_mcode.c
@@ -7,18 +7,65 @@
7#define LUA_CORE 7#define LUA_CORE
8 8
9#include "lj_obj.h" 9#include "lj_obj.h"
10
11#if LJ_HASJIT 10#if LJ_HASJIT
12
13#include "lj_gc.h" 11#include "lj_gc.h"
14#include "lj_jit.h" 12#include "lj_jit.h"
15#include "lj_mcode.h" 13#include "lj_mcode.h"
16#include "lj_trace.h" 14#include "lj_trace.h"
17#include "lj_dispatch.h" 15#include "lj_dispatch.h"
18#include "lj_vm.h" 16#include "lj_vm.h"
17#endif
19 18
20/* -- OS-specific functions ----------------------------------------------- */ 19/* -- OS-specific functions ----------------------------------------------- */
21 20
21#if LJ_HASJIT || LJ_HASFFI
22
23/* Define this if you want to run LuaJIT with Valgrind. */
24#ifdef LUAJIT_USE_VALGRIND
25#include <valgrind/valgrind.h>
26#endif
27
28#if !LJ_TARGET_X86ORX64 && LJ_TARGET_OSX
29void sys_icache_invalidate(void *start, size_t len);
30#endif
31
32#if LJ_TARGET_LINUX && LJ_TARGET_PPC
33#include <dlfcn.h>
34static void (*mcode_sync_ppc)(void *start, void *end);
35static void mcode_sync_dummy(void *start, void *end)
36{
37 UNUSED(start); UNUSED(end);
38}
39#endif
40
41/* Synchronize data/instruction cache. */
42void lj_mcode_sync(void *start, void *end)
43{
44#ifdef LUAJIT_USE_VALGRIND
45 VALGRIND_DISCARD_TRANSLATIONS(start, (char *)end-(char *)start);
46#endif
47#if LJ_TARGET_X86ORX64
48 UNUSED(start); UNUSED(end);
49#elif LJ_TARGET_OSX
50 sys_icache_invalidate(start, (char *)end-(char *)start);
51#elif LJ_TARGET_LINUX && LJ_TARGET_PPC
52 if (!mcode_sync_ppc) {
53 void *vdso = dlopen("linux-vdso32.so.1", RTLD_LAZY);
54 if (!vdso || !(mcode_sync_ppc = dlsym(vdso, "__kernel_sync_dicache")))
55 mcode_sync_ppc = mcode_sync_dummy;
56 }
57 mcode_sync_ppc(start, end);
58#elif defined(__GNUC__) && !LJ_TARGET_PPC
59 __clear_cache(start, end);
60#else
61#error "Missing builtin to flush instruction cache"
62#endif
63}
64
65#endif
66
67#if LJ_HASJIT
68
22#if LJ_TARGET_WINDOWS 69#if LJ_TARGET_WINDOWS
23 70
24#define WIN32_LEAN_AND_MEAN 71#define WIN32_LEAN_AND_MEAN
diff --git a/src/lj_mcode.h b/src/lj_mcode.h
index be1bdb5a..d11b6682 100644
--- a/src/lj_mcode.h
+++ b/src/lj_mcode.h
@@ -6,9 +6,16 @@
6#ifndef _LJ_MCODE_H 6#ifndef _LJ_MCODE_H
7#define _LJ_MCODE_H 7#define _LJ_MCODE_H
8 8
9#include "lj_jit.h" 9#include "lj_obj.h"
10
11#if LJ_HASJIT || LJ_HASFFI
12LJ_FUNC void lj_mcode_sync(void *start, void *end);
13#endif
10 14
11#if LJ_HASJIT 15#if LJ_HASJIT
16
17#include "lj_jit.h"
18
12LJ_FUNC void lj_mcode_free(jit_State *J); 19LJ_FUNC void lj_mcode_free(jit_State *J);
13LJ_FUNC MCode *lj_mcode_reserve(jit_State *J, MCode **lim); 20LJ_FUNC MCode *lj_mcode_reserve(jit_State *J, MCode **lim);
14LJ_FUNC void lj_mcode_commit(jit_State *J, MCode *m); 21LJ_FUNC void lj_mcode_commit(jit_State *J, MCode *m);