vx32

Local 9vx git repository for patches.
git clone git://r-36.net/vx32
Log | Files | Refs

run32.S (2766B)


      1 //
      2 // Assembly-language support code for vx32-to-x86-32 translation
      3 //
      4 
      5 #include "libvx32/asm.h"
      6 #include "libvx32/os.h"
      7 
      8 	.text
      9 
     10 	.globl	EXT(vx_run_S_start)
     11 EXT(vx_run_S_start):
     12 
     13 // Perform setup necessary for the emulation environment.
     14 // Args:
     15 //	1. vxemu pointer
     16 //
     17 	.globl	EXT(vxrun_setup)
     18 EXT(vxrun_setup):
     19 
     20 	// Load vxemu pointer
     21 	movl	4(%esp),%eax
     22 
     23 	// Save the host's normal segment registers.
     24 	movw	%ss,%dx
     25 	movw	%dx,VXEMU_HOST_SS(%eax)
     26 	movw	%ds,VXEMU_HOST_DS(%eax)
     27 	movw	%es,VXEMU_HOST_ES(%eax)
     28 	movw	VSEG,VXEMU_HOST_VS(%eax)
     29 
     30 	// Load the special vxemu segment into VSEG (%fs or %gs)
     31 	movw	VXEMU_EMUSEL(%eax),VSEG
     32 
     33 	ret
     34 
     35 
     36 // Start running translated vx32 code until something goes wrong -
     37 // usually, until we hit a piece of code that hasn't been translated yet.
     38 //
     39 // Args:
     40 //	1. vxemu pointer
     41 //	2. translated code entrypoint at which to start running
     42 //
     43 	.p2align 4
     44 	.globl	EXT(vxrun)
     45 EXT(vxrun):
     46 
     47 	// Save caller's host registers
     48 	pushl	%ebx
     49 	pushl	%esi
     50 	pushl	%edi
     51 	pushl	%ebp
     52 
     53 	// Fetch translated code entrypoint arg
     54 	movl	(4*4)+4+4(%esp),%ebx
     55 
     56 	// Save host esp
     57 	movl	%esp,VSEG:VXEMU_HOST_ESP
     58 
     59 	// Restore vx32 env's eflags register
     60 	pushl	VSEG:VXEMU_EFLAGS
     61 	popfl
     62 
     63 	// Set up segment registers for vx32 env
     64 	movl	VSEG:VXEMU_DATASEL,%ecx
     65 	movw	%cx,%ds
     66 	movw	%cx,%es
     67 	movw	%cx,%ss
     68 
     69 	// Load vx32 env's registers
     70 	movl	VSEG:VXEMU_EAX,%eax
     71 	movl	VSEG:VXEMU_ECX,%ecx
     72 	movl	VSEG:VXEMU_EDX,%edx
     73 	// translated code will restore %EBX
     74 	movl	VSEG:VXEMU_ESP,%esp
     75 	movl	VSEG:VXEMU_EBP,%ebp
     76 	movl	VSEG:VXEMU_ESI,%esi
     77 	movl	VSEG:VXEMU_EDI,%edi
     78 
     79 	// Run translated code
     80 	jmp	*%ebx
     81 
     82 
     83 // Return from running translated code to the normal host environment.
     84 // Assumes EAX, EBX, ECX, and EDX have already been saved.
     85 // Assumes return code for vxrun is already in eax.
     86 //
     87 	.p2align 4
     88 	.globl	EXT(vxrun_return)
     89 EXT(vxrun_return):
     90 
     91 	// Save remaining vx32 registers
     92 	movl	%esp,VSEG:VXEMU_ESP
     93 	movl	%ebp,VSEG:VXEMU_EBP
     94 	movl	%esi,VSEG:VXEMU_ESI
     95 	movl	%edi,VSEG:VXEMU_EDI
     96 
     97 	// Restore host's normal segment registers
     98 	movw	VSEG:VXEMU_HOST_DS,%ds
     99 	movw	VSEG:VXEMU_HOST_ES,%es
    100 	movw	VSEG:VXEMU_HOST_SS,%ss
    101 
    102 	// Switch back to host's stack
    103 	movl	VSEG:VXEMU_HOST_ESP,%esp
    104 
    105 	// Save vx32 env's eflags register
    106 	pushfl
    107 	popl	VSEG:VXEMU_EFLAGS
    108 
    109 	// Restore host's callee-save registers and return to caller
    110 	popl	%ebp
    111 	popl	%edi
    112 	popl	%esi
    113 	popl	%ebx
    114 	cld
    115 	ret
    116 
    117 
    118 // Clean up after a stint of running VX code.
    119 // Args:
    120 //	1. vxemu pointer
    121 //
    122 	.globl	EXT(vxrun_cleanup)
    123 EXT(vxrun_cleanup):
    124 
    125 	// Load vxemu pointer
    126 	movl	4(%esp),%eax
    127 
    128 	// Restore host's FS/GS register.
    129 	// (DS/ES/SS were already restored by vxrun_return.)
    130 	movw	VXEMU_HOST_VS(%eax),VSEG
    131 
    132 	cld
    133 	ret
    134 	
    135 // Don't put anything here!
    136 // The signal handler knows that vxrun_cleanup
    137 // is at the bottom of this file.