8

In this page, http://www.x86-64.org/pipermail/discuss/2004-August/005020.html He said that there is a way to mix 32-bit code and 64-bit code in a application. He assumed the application is 32-bit (in compatibility mode) and then switch to 64-bit mode to execute 64-bit code and vice versa.

Assume my OS is 64-bit linux and my application is 64-bit. I do a far jump to switch to compatibility mode and execute 32-bit code. Does it can work correctly when I do a system call or function call ?

Is there any overhead of mode switching between compatibility mode and 64-bit mode ? I think one of the overhead is I need separate stack for 32-bit and 64-bit.

Could I integrate this idea into JVM, maybe I can dynamic generate 32-bit code in 64-bit JVM, and execute it by mode switching ?

Hsiao-Hui Chiu
  • 191
  • 2
  • 9
  • If you are using a 64-bit JVM, why would you want to generate 32-bit code? You can do all of the 32-bit operations you want (almost) in 64-bit code; most instructions default to 32-bit operands even in 64-bit mode. – Jeremiah Willcock Feb 22 '11 at 16:58
  • 1
    Because I want to compare the difference between 32-bit code and 64-bit code. Maybe I can get difference in some case, like the size of long type, or the execute time for each case. – Hsiao-Hui Chiu Feb 22 '11 at 17:36
  • Read the post again, it suggest modifying the CS -- this would invoke custom `ld.so` and stuff like that. Make sure you are prepared for that. – J-16 SDiZ Feb 23 '11 at 07:01

1 Answers1

1

Open-coded syscalls should be fine, since your 32-bit code will use the 32-bit kernel entry point.

Function calls can only be made to other 32-bit code, of course. This includes libc - so your 32-bit code will either have to be self-contained, or you will have to provide thunks for the library functions that it needs. Remember that usually syscalls are not called directly - you normally go via a libc wrapper that will be unavailable to your 32-bit code.

There is certainly an overhead for switching between modes. You should consult your processor documentation to find out what it is.

caf
  • 233,326
  • 40
  • 323
  • 462
  • In your opinion, I have to prepare 2 different version of libc (32-bit and 64-bit), but is it possible to load 32-bit and 64-bit libraries into a executable, even dynamic load using dlopen? I think it not possible. – Hsiao-Hui Chiu Mar 04 '11 at 04:23
  • 1
    @Hsiao-Hui Chiu: No, it generally wouldn't be possible, and that's not what you want to do anyway. Two different `libc` would mean two sets of conflicting stdio buffers and similar problems. Instead, you would need to write 32 bit thunks that switched back to long mode and then called into the 64 bit libc, including the necessary shuffling of parameters and results. – caf Mar 06 '11 at 11:22