From c0a81a03ee1b0eb207a578bd8c96e804b81fdcbe Mon Sep 17 00:00:00 2001 From: Ilya Berezhniuk Date: Thu, 20 Mar 2008 02:22:30 +0300 Subject: [PATCH] Make-Port-Crash-Signal-Handler-shared v3 - 01-move renamed: vm/port/src/thread/linux/thread_asm_em64t.s -> vm/port/src/signals/linux/signals_asm_em64t.s renamed: vm/port/src/thread/linux/thread_asm_ia32.s -> vm/port/src/signals/linux/signals_asm_ia32.s renamed: vm/port/src/signals/win/signals_em64t.asm -> vm/port/src/signals/win/signals_asm_em64t.asm renamed: vm/port/src/thread/win/thread_asm_ia32.asm -> vm/port/src/signals/win/signals_asm_ia32.asm --- vm/port/src/signals/linux/signals_asm_em64t.s | 120 +++++++++++++++++++++++++ vm/port/src/signals/linux/signals_asm_ia32.s | 96 ++++++++++++++++++++ vm/port/src/signals/win/signals_asm_em64t.asm | 27 ++++++ vm/port/src/signals/win/signals_asm_ia32.asm | 108 ++++++++++++++++++++++ vm/port/src/signals/win/signals_em64t.asm | 27 ------ vm/port/src/thread/linux/thread_asm_em64t.s | 120 ------------------------- vm/port/src/thread/linux/thread_asm_ia32.s | 96 -------------------- vm/port/src/thread/win/thread_asm_ia32.asm | 108 ---------------------- 8 files changed, 351 insertions(+), 351 deletions(-) create mode 100644 vm/port/src/signals/linux/signals_asm_em64t.s create mode 100644 vm/port/src/signals/linux/signals_asm_ia32.s create mode 100644 vm/port/src/signals/win/signals_asm_em64t.asm create mode 100644 vm/port/src/signals/win/signals_asm_ia32.asm delete mode 100644 vm/port/src/signals/win/signals_em64t.asm delete mode 100644 vm/port/src/thread/linux/thread_asm_em64t.s delete mode 100644 vm/port/src/thread/linux/thread_asm_ia32.s delete mode 100644 vm/port/src/thread/win/thread_asm_ia32.asm diff --git a/vm/port/src/signals/linux/signals_asm_em64t.s b/vm/port/src/signals/linux/signals_asm_em64t.s new file mode 100644 index 0000000..5468020 --- /dev/null +++ b/vm/port/src/signals/linux/signals_asm_em64t.s @@ -0,0 +1,120 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + .text + .align 16 + +// struct Registers { +// uint64 rsp; ; 00h +// uint64 rbp; ; 08h +// uint64 rip; ; 10h +// // callee-saved +// uint64 rbx; ; 18h +// uint64 r12; ; 20h +// uint64 r13; ; 28h +// uint64 r14; ; 30h +// uint64 r15; ; 38h +// // scratched +// uint64 rax; ; 40h +// uint64 rcx; ; 48h +// uint64 rdx; ; 50h +// uint64 rsi; ; 58h +// uint64 rdi; ; 60h +// uint64 r8; ; 68h +// uint64 r9; ; 70h +// uint64 r10; ; 78h +// uint64 r11; ; 80h +// +// uint32 eflags;; 88h +// }; +// +// void port_transfer_to_regs(Registers* regs) + +.globl port_transfer_to_regs + .type port_transfer_to_regs, @function +port_transfer_to_regs: + movq %rdi, %rdx // regs pointer (1st param - RDI) -> RDX + + movq 0x08(%rdx), %rbp // RBP field + movq 0x18(%rdx), %rbx // RBX field + movq 0x20(%rdx), %r12 // R12 field + movq 0x28(%rdx), %r13 // R13 field + movq 0x30(%rdx), %r14 // R14 field + movq 0x38(%rdx), %r15 // R15 field + movq 0x58(%rdx), %rsi // RSI field + movq 0x60(%rdx), %rdi // RDI field + movq 0x68(%rdx), %r8 // R8 field + movq 0x70(%rdx), %r9 // R9 field + movq 0x78(%rdx), %r10 // R10 field + movq 0x80(%rdx), %r11 // R11 field + + movq 0x00(%rdx), %rax // (new RSP) -> RAX + movq %rax, (%rsp) // (new RSP) -> [RSP] for future use + movq 0x10(%rdx), %rcx // (new RIP) -> RCX + movq %rcx, -0x88(%rax)// (new RIP) -> [(new RSP) - 128 - 8] + movq 0x40(%rdx), %rax // RAX field + + movzwq 0x88(%rdx), %rcx // (word)EFLAGS -> RCX + test %rcx, %rcx + je __skipefl__ + pushfq + andl $0x003F7202, (%rsp) // Clear OF, DF, TF, SF, ZF, AF, PF, CF + andl $0x00000CD5, %ecx // Clear all except OF, DF, SF, ZF, AF, PF, CF + orl %ecx, (%rsp) + popfq // restore RFLAGS +__skipefl__: + + movq 0x48(%rdx), %rcx // RCX field + movq 0x50(%rdx), %rdx // RDX field + + movq (%rsp), %rsp // load new RSP + jmpq * -0x88(%rsp) // JMP to new RIP + + +// void port_longjump_stub(void) +// +// after returning from the called function, RSP points to the 2 argument +// slots in the stack. Saved Registers structure pointer is (RSP + 48) +// +// | interrupted | +// | program | <- RSP where the program was interrupted by signal +// |-------------| +// | 0x80 bytes | <- 'red zone' - we will not change it +// |-------------| +// | return addr | +// | from stub | <- for using in port_transfer_to_regs as [(new RSP) - 128 - 8] +// |-------------| +// | saved | +// | Registers | <- to restore register context +// |-------------| +// | [alignment] | <- align Regs pointer to 16-bytes boundary +// |-------------| +// | pointer to | +// | saved Regs | <- (RSP + 128) +// // |-------------| +// // | 0x80 bytes | <- 'red zone' +// |-------------| +// | return addr | +// | from 'fn' | <- address to return to the port_longjump_stub +// |-------------| + +.globl port_longjump_stub + .type port_longjump_stub, @function +port_longjump_stub: +// movq 128(%rsp), %rdi // load RDI with the address of saved Registers + movq (%rsp), %rdi // load RDI with the address of saved Registers + callq port_transfer_to_regs // restore context + ret // dummy RET - unreachable diff --git a/vm/port/src/signals/linux/signals_asm_ia32.s b/vm/port/src/signals/linux/signals_asm_ia32.s new file mode 100644 index 0000000..fd15aaf --- /dev/null +++ b/vm/port/src/signals/linux/signals_asm_ia32.s @@ -0,0 +1,96 @@ +// +// Licensed to the Apache Software Foundation (ASF) under one or more +// contributor license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright ownership. +// The ASF licenses this file to You under the Apache License, Version 2.0 +// (the "License"); you may not use this file except in compliance with +// the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + + .text + .align 4 + +// struct Registers { +// uint32 eax; +00 +// uint32 ebx; +04 +// uint32 ecx; +08 +// uint32 edx; +0C +// uint32 edi; +10 +// uint32 esi; +14 +// uint32 ebp; +18 +// uint32 esp; +1C +// uint32 eip; +20 +// uint32 eflags; +24 +// }; +// +// void port_transfer_to_regs(Registers* regs) + +.globl port_transfer_to_regs + .type port_transfer_to_regs, @function +port_transfer_to_regs: + movl 0x04(%esp), %edx // store regs pointer to EDX + movl 0x20(%edx), %ebx // EIP field -> EBX + movl 0x1C(%edx), %ecx // ESP field + subl $4, %ecx + movl %ecx, 0x04(%esp) // (new ESP - 4) -> [ESP + 4] (safe storage) + movl 0x14(%edx), %esi // ESI field + movl 0x10(%edx), %edi // EDI field + movl 0x18(%edx), %ebp // EBP field + movl %ebx, (%ecx) // new EIP -> (new ESP - 4) (as return address) + movl 0x00(%edx), %eax // EAX field + movl 0x04(%edx), %ebx // EBX field + movzwl 0x24(%edx), %ecx // (word)EFLAGS -> ECX + test %ecx, %ecx + je _label_ + pushfl + andl $0x003F7202, (%esp) + andl $0x00000CD5, %ecx + orl %ecx, (%esp) + popfl // restore EFLAGS +_label_: + movl 0x08(%edx), %ecx // ECX field + movl 0x0C(%edx), %edx // EDX field + movl 0x04(%esp), %esp // ((new ESP - 4) -> ESP + ret // JMP by RET + + +// void port_longjump_stub(void) +// +// after returning from the called function, EBP points to the pointer +// to saved Registers structure +// +// | interrupted | +// | program | <- ESP where the program was interrupted by signal +// |-------------| +// | return addr | +// | from stub | <- for using in port_transfer_to_regs +// |-------------| +// | saved | +// | Registers | <- to restore register context +// |-------------| +// | pointer to | +// | saved Regs | <- EBP +// |-------------| +// | arg 5 | <- +// |-------------| | +// ............... - arguments for 'fn' +// |-------------| | +// | arg 0 | <- +// |-------------| +// | return addr | +// | from 'fn' | <- address to return to the port_longjump_stub +// |-------------| + +.globl port_longjump_stub + .type port_longjump_stub, @function +port_longjump_stub: + movl %ebp, %esp // ESP now points to the address of saved Registers + call port_transfer_to_regs // restore context + ret // dummy RET - unreachable diff --git a/vm/port/src/signals/win/signals_asm_em64t.asm b/vm/port/src/signals/win/signals_asm_em64t.asm new file mode 100644 index 0000000..b54aa4f --- /dev/null +++ b/vm/port/src/signals/win/signals_asm_em64t.asm @@ -0,0 +1,27 @@ +PUBLIC vectored_exception_handler +EXTRN vectored_exception_handler_internal:PROC + +_TEXT SEGMENT + +vectored_exception_handler PROC + +; LONG NTAPI vectored_exception_handler(LPEXCEPTION_POINTERS nt_exception) +; Args: +; rcx - nt_exception +; rdx - none +; r8 - none +; r9 - none + + pushfq + cld + sub rsp, 32 ; allocate stack for 4 registers + call vectored_exception_handler_internal + add rsp, 32 + popfq + ret + +vectored_exception_handler ENDP + +_TEXT ENDS + +END diff --git a/vm/port/src/signals/win/signals_asm_ia32.asm b/vm/port/src/signals/win/signals_asm_ia32.asm new file mode 100644 index 0000000..b3b4adb --- /dev/null +++ b/vm/port/src/signals/win/signals_asm_ia32.asm @@ -0,0 +1,108 @@ +; +; Licensed to the Apache Software Foundation (ASF) under one or more +; contributor license agreements. See the NOTICE file distributed with +; this work for additional information regarding copyright ownership. +; The ASF licenses this file to You under the Apache License, Version 2.0 +; (the "License"); you may not use this file except in compliance with +; the License. You may obtain a copy of the License at +; +; http://www.apache.org/licenses/LICENSE-2.0 +; +; Unless required by applicable law or agreed to in writing, software +; distributed under the License is distributed on an "AS IS" BASIS, +; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +; See the License for the specific language governing permissions and +; limitations under the License. + + .386P + +_TEXT SEGMENT PARA USE32 PUBLIC 'CODE' + +; struct Registers { +; uint32 eax; +00 +; uint32 ebx; +04 +; uint32 ecx; +08 +; uint32 edx; +0C +; uint32 edi; +10 +; uint32 esi; +14 +; uint32 ebp; +18 +; uint32 esp; +1C +; uint32 eip; +20 +; uint32 eflags; +24 +; }; +; +; void port_transfer_to_regs(Registers* regs) + +PUBLIC port_transfer_to_regs + +port_transfer_to_regs PROC + + mov edx, dword ptr [esp+04h] ; store regs pointer to EDX + mov ebx, dword ptr [edx+20h] ; EIP field -> EBX + mov ecx, dword ptr [edx+1Ch] ; ESP field + sub ecx,4 + mov dword ptr [esp+04h], ecx ; (new ESP - 4) -> [ESP + 4] (safe storage) + mov esi, dword ptr [edx+14h] ; ESI field + mov edi, dword ptr [edx+10h] ; EDI field + mov ebp, dword ptr [edx+18h] ; EBP field + mov dword ptr [ecx], ebx ; new EIP -> (new ESP - 4) (as return address) + mov eax, dword ptr [edx+00h] ; EAX field + mov ebx, dword ptr [edx+04h] ; EBX field + movzx ecx, word ptr [edx+24h] ; (word)EFLAGS -> ECX + test ecx, ecx + je _label_ + pushfd + and dword ptr [esp], 003F7202h ; Clear OF, DF, TF, SF, ZF, AF, PF, CF + and ecx, 00000CD5h ; Clear all except OF, DF, SF, ZF, AF, PF, CF + or dword ptr [esp], ecx + popfd ; restore EFLAGS +_label_: + mov ecx, dword ptr [edx+08h] ; ECX field + mov edx, dword ptr [edx+0Ch] ; EDX field + mov esp, dword ptr [esp+04h] ; ((new ESP - 4) -> ESP + ret ; JMP by RET + +port_transfer_to_regs ENDP + + +; void port_longjump_stub(void) +; +; after returning from the called function, EBP points to the pointer +; to saved Registers structure +; +; | interrupted | +; | program | <- ESP where the program was interrupted by exception +; |-------------| +; | return addr | +; | from stub | <- for using in port_transfer_to_regs +; |-------------| +; | saved | +; | Registers | <- to restore register context +; |-------------| +; | pointer to | +; | saved Regs | <- EBP +; |-------------| +; | arg 5 | <- +; |-------------| | +; ............... - arguments for 'fn' +; |-------------| | +; | arg 0 | <- +; |-------------| +; | return addr | +; | from 'fn' | <- address to return to the port_longjump_stub +; |-------------| + +PUBLIC port_longjump_stub + +port_longjump_stub PROC + + mov esp, ebp ; ESP now points to the address of saved Registers + call port_transfer_to_regs ; restore context + ret ; dummy RET - unreachable +port_longjump_stub ENDP + + +_TEXT ENDS + +END + diff --git a/vm/port/src/signals/win/signals_em64t.asm b/vm/port/src/signals/win/signals_em64t.asm deleted file mode 100644 index b54aa4f..0000000 --- a/vm/port/src/signals/win/signals_em64t.asm +++ /dev/null @@ -1,27 +0,0 @@ -PUBLIC vectored_exception_handler -EXTRN vectored_exception_handler_internal:PROC - -_TEXT SEGMENT - -vectored_exception_handler PROC - -; LONG NTAPI vectored_exception_handler(LPEXCEPTION_POINTERS nt_exception) -; Args: -; rcx - nt_exception -; rdx - none -; r8 - none -; r9 - none - - pushfq - cld - sub rsp, 32 ; allocate stack for 4 registers - call vectored_exception_handler_internal - add rsp, 32 - popfq - ret - -vectored_exception_handler ENDP - -_TEXT ENDS - -END diff --git a/vm/port/src/thread/linux/thread_asm_em64t.s b/vm/port/src/thread/linux/thread_asm_em64t.s deleted file mode 100644 index 5468020..0000000 --- a/vm/port/src/thread/linux/thread_asm_em64t.s +++ /dev/null @@ -1,120 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one or more -// contributor license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright ownership. -// The ASF licenses this file to You under the Apache License, Version 2.0 -// (the "License"); you may not use this file except in compliance with -// the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - - .text - .align 16 - -// struct Registers { -// uint64 rsp; ; 00h -// uint64 rbp; ; 08h -// uint64 rip; ; 10h -// // callee-saved -// uint64 rbx; ; 18h -// uint64 r12; ; 20h -// uint64 r13; ; 28h -// uint64 r14; ; 30h -// uint64 r15; ; 38h -// // scratched -// uint64 rax; ; 40h -// uint64 rcx; ; 48h -// uint64 rdx; ; 50h -// uint64 rsi; ; 58h -// uint64 rdi; ; 60h -// uint64 r8; ; 68h -// uint64 r9; ; 70h -// uint64 r10; ; 78h -// uint64 r11; ; 80h -// -// uint32 eflags;; 88h -// }; -// -// void port_transfer_to_regs(Registers* regs) - -.globl port_transfer_to_regs - .type port_transfer_to_regs, @function -port_transfer_to_regs: - movq %rdi, %rdx // regs pointer (1st param - RDI) -> RDX - - movq 0x08(%rdx), %rbp // RBP field - movq 0x18(%rdx), %rbx // RBX field - movq 0x20(%rdx), %r12 // R12 field - movq 0x28(%rdx), %r13 // R13 field - movq 0x30(%rdx), %r14 // R14 field - movq 0x38(%rdx), %r15 // R15 field - movq 0x58(%rdx), %rsi // RSI field - movq 0x60(%rdx), %rdi // RDI field - movq 0x68(%rdx), %r8 // R8 field - movq 0x70(%rdx), %r9 // R9 field - movq 0x78(%rdx), %r10 // R10 field - movq 0x80(%rdx), %r11 // R11 field - - movq 0x00(%rdx), %rax // (new RSP) -> RAX - movq %rax, (%rsp) // (new RSP) -> [RSP] for future use - movq 0x10(%rdx), %rcx // (new RIP) -> RCX - movq %rcx, -0x88(%rax)// (new RIP) -> [(new RSP) - 128 - 8] - movq 0x40(%rdx), %rax // RAX field - - movzwq 0x88(%rdx), %rcx // (word)EFLAGS -> RCX - test %rcx, %rcx - je __skipefl__ - pushfq - andl $0x003F7202, (%rsp) // Clear OF, DF, TF, SF, ZF, AF, PF, CF - andl $0x00000CD5, %ecx // Clear all except OF, DF, SF, ZF, AF, PF, CF - orl %ecx, (%rsp) - popfq // restore RFLAGS -__skipefl__: - - movq 0x48(%rdx), %rcx // RCX field - movq 0x50(%rdx), %rdx // RDX field - - movq (%rsp), %rsp // load new RSP - jmpq * -0x88(%rsp) // JMP to new RIP - - -// void port_longjump_stub(void) -// -// after returning from the called function, RSP points to the 2 argument -// slots in the stack. Saved Registers structure pointer is (RSP + 48) -// -// | interrupted | -// | program | <- RSP where the program was interrupted by signal -// |-------------| -// | 0x80 bytes | <- 'red zone' - we will not change it -// |-------------| -// | return addr | -// | from stub | <- for using in port_transfer_to_regs as [(new RSP) - 128 - 8] -// |-------------| -// | saved | -// | Registers | <- to restore register context -// |-------------| -// | [alignment] | <- align Regs pointer to 16-bytes boundary -// |-------------| -// | pointer to | -// | saved Regs | <- (RSP + 128) -// // |-------------| -// // | 0x80 bytes | <- 'red zone' -// |-------------| -// | return addr | -// | from 'fn' | <- address to return to the port_longjump_stub -// |-------------| - -.globl port_longjump_stub - .type port_longjump_stub, @function -port_longjump_stub: -// movq 128(%rsp), %rdi // load RDI with the address of saved Registers - movq (%rsp), %rdi // load RDI with the address of saved Registers - callq port_transfer_to_regs // restore context - ret // dummy RET - unreachable diff --git a/vm/port/src/thread/linux/thread_asm_ia32.s b/vm/port/src/thread/linux/thread_asm_ia32.s deleted file mode 100644 index fd15aaf..0000000 --- a/vm/port/src/thread/linux/thread_asm_ia32.s +++ /dev/null @@ -1,96 +0,0 @@ -// -// Licensed to the Apache Software Foundation (ASF) under one or more -// contributor license agreements. See the NOTICE file distributed with -// this work for additional information regarding copyright ownership. -// The ASF licenses this file to You under the Apache License, Version 2.0 -// (the "License"); you may not use this file except in compliance with -// the License. You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - - .text - .align 4 - -// struct Registers { -// uint32 eax; +00 -// uint32 ebx; +04 -// uint32 ecx; +08 -// uint32 edx; +0C -// uint32 edi; +10 -// uint32 esi; +14 -// uint32 ebp; +18 -// uint32 esp; +1C -// uint32 eip; +20 -// uint32 eflags; +24 -// }; -// -// void port_transfer_to_regs(Registers* regs) - -.globl port_transfer_to_regs - .type port_transfer_to_regs, @function -port_transfer_to_regs: - movl 0x04(%esp), %edx // store regs pointer to EDX - movl 0x20(%edx), %ebx // EIP field -> EBX - movl 0x1C(%edx), %ecx // ESP field - subl $4, %ecx - movl %ecx, 0x04(%esp) // (new ESP - 4) -> [ESP + 4] (safe storage) - movl 0x14(%edx), %esi // ESI field - movl 0x10(%edx), %edi // EDI field - movl 0x18(%edx), %ebp // EBP field - movl %ebx, (%ecx) // new EIP -> (new ESP - 4) (as return address) - movl 0x00(%edx), %eax // EAX field - movl 0x04(%edx), %ebx // EBX field - movzwl 0x24(%edx), %ecx // (word)EFLAGS -> ECX - test %ecx, %ecx - je _label_ - pushfl - andl $0x003F7202, (%esp) - andl $0x00000CD5, %ecx - orl %ecx, (%esp) - popfl // restore EFLAGS -_label_: - movl 0x08(%edx), %ecx // ECX field - movl 0x0C(%edx), %edx // EDX field - movl 0x04(%esp), %esp // ((new ESP - 4) -> ESP - ret // JMP by RET - - -// void port_longjump_stub(void) -// -// after returning from the called function, EBP points to the pointer -// to saved Registers structure -// -// | interrupted | -// | program | <- ESP where the program was interrupted by signal -// |-------------| -// | return addr | -// | from stub | <- for using in port_transfer_to_regs -// |-------------| -// | saved | -// | Registers | <- to restore register context -// |-------------| -// | pointer to | -// | saved Regs | <- EBP -// |-------------| -// | arg 5 | <- -// |-------------| | -// ............... - arguments for 'fn' -// |-------------| | -// | arg 0 | <- -// |-------------| -// | return addr | -// | from 'fn' | <- address to return to the port_longjump_stub -// |-------------| - -.globl port_longjump_stub - .type port_longjump_stub, @function -port_longjump_stub: - movl %ebp, %esp // ESP now points to the address of saved Registers - call port_transfer_to_regs // restore context - ret // dummy RET - unreachable diff --git a/vm/port/src/thread/win/thread_asm_ia32.asm b/vm/port/src/thread/win/thread_asm_ia32.asm deleted file mode 100644 index b3b4adb..0000000 --- a/vm/port/src/thread/win/thread_asm_ia32.asm +++ /dev/null @@ -1,108 +0,0 @@ -; -; Licensed to the Apache Software Foundation (ASF) under one or more -; contributor license agreements. See the NOTICE file distributed with -; this work for additional information regarding copyright ownership. -; The ASF licenses this file to You under the Apache License, Version 2.0 -; (the "License"); you may not use this file except in compliance with -; the License. You may obtain a copy of the License at -; -; http://www.apache.org/licenses/LICENSE-2.0 -; -; Unless required by applicable law or agreed to in writing, software -; distributed under the License is distributed on an "AS IS" BASIS, -; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -; See the License for the specific language governing permissions and -; limitations under the License. - - .386P - -_TEXT SEGMENT PARA USE32 PUBLIC 'CODE' - -; struct Registers { -; uint32 eax; +00 -; uint32 ebx; +04 -; uint32 ecx; +08 -; uint32 edx; +0C -; uint32 edi; +10 -; uint32 esi; +14 -; uint32 ebp; +18 -; uint32 esp; +1C -; uint32 eip; +20 -; uint32 eflags; +24 -; }; -; -; void port_transfer_to_regs(Registers* regs) - -PUBLIC port_transfer_to_regs - -port_transfer_to_regs PROC - - mov edx, dword ptr [esp+04h] ; store regs pointer to EDX - mov ebx, dword ptr [edx+20h] ; EIP field -> EBX - mov ecx, dword ptr [edx+1Ch] ; ESP field - sub ecx,4 - mov dword ptr [esp+04h], ecx ; (new ESP - 4) -> [ESP + 4] (safe storage) - mov esi, dword ptr [edx+14h] ; ESI field - mov edi, dword ptr [edx+10h] ; EDI field - mov ebp, dword ptr [edx+18h] ; EBP field - mov dword ptr [ecx], ebx ; new EIP -> (new ESP - 4) (as return address) - mov eax, dword ptr [edx+00h] ; EAX field - mov ebx, dword ptr [edx+04h] ; EBX field - movzx ecx, word ptr [edx+24h] ; (word)EFLAGS -> ECX - test ecx, ecx - je _label_ - pushfd - and dword ptr [esp], 003F7202h ; Clear OF, DF, TF, SF, ZF, AF, PF, CF - and ecx, 00000CD5h ; Clear all except OF, DF, SF, ZF, AF, PF, CF - or dword ptr [esp], ecx - popfd ; restore EFLAGS -_label_: - mov ecx, dword ptr [edx+08h] ; ECX field - mov edx, dword ptr [edx+0Ch] ; EDX field - mov esp, dword ptr [esp+04h] ; ((new ESP - 4) -> ESP - ret ; JMP by RET - -port_transfer_to_regs ENDP - - -; void port_longjump_stub(void) -; -; after returning from the called function, EBP points to the pointer -; to saved Registers structure -; -; | interrupted | -; | program | <- ESP where the program was interrupted by exception -; |-------------| -; | return addr | -; | from stub | <- for using in port_transfer_to_regs -; |-------------| -; | saved | -; | Registers | <- to restore register context -; |-------------| -; | pointer to | -; | saved Regs | <- EBP -; |-------------| -; | arg 5 | <- -; |-------------| | -; ............... - arguments for 'fn' -; |-------------| | -; | arg 0 | <- -; |-------------| -; | return addr | -; | from 'fn' | <- address to return to the port_longjump_stub -; |-------------| - -PUBLIC port_longjump_stub - -port_longjump_stub PROC - - mov esp, ebp ; ESP now points to the address of saved Registers - call port_transfer_to_regs ; restore context - ret ; dummy RET - unreachable -port_longjump_stub ENDP - - -_TEXT ENDS - -END - -- 1.5.4