↑ Back to main site.

EPOS

Experimental Protected-mode Operating System

processa.asm
; EPOS
; http://www.atanaslaskov.com/epos/
;
; FILE: processa.asm
; DESCRIPTION:
; Implements the function tmIRetToProcess that switches from the EPOS kernel
; to an user process.
;
; BSD LICENSE
; Copyright (c) 2006, Atanas Laskov
; All rights reserved.
; Redistribution and use in source and binary forms, with or without
; modification, are permitted provided that the following conditions are met:
;   1. Redistributions of source code must retain the above copyright notice,
;   this list of conditions and the following disclaimer.
;   2. Redistributions in binary form must reproduce the above copyright notice,
;   this list of conditions and the following disclaimer in the documentation
;   and/or other materials provided with the distribution.
; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
; DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
; (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
; (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
;

extern SEL_GDT_LDT0
extern SEL_LDT_DATA
extern SEL_LDT_CODE

; void tmIRetToProcess(unsigned long nldt, struct ProcessState *pRecoveryState)
;
global _tmIRetToProcess
_tmIRetToProcess:
    push ebp
    mov ebp, esp

    ; Switch to process LDT
    ;
    mov eax, [ebp+8]
    mov bl, 8
    mul bl
    add ax, SEL_GDT_LDT0
    lldt ax
    mov ax, SEL_LDT_DATA
    mov ds, ax
    mov es, ax
    ;mov fs, ax
    mov gs, ax

    ; Load process context
    ;
    mov esi, [ebp+12]
    mov eax, [fs:esi+0*4]
    mov ebx, [fs:esi+1*4]
    mov ecx, [fs:esi+2*4]
    mov edx, [fs:esi+3*4]
    ;eflags later +4*4
    mov ebp, [fs:esi+5*4]
    mov edi, [fs:esi+6*4]
    ;esi later +7*4
    ;esp later +8*4
    ;eip later +9*4

    ; Jump to IE32 ring 0 (user mode)
    ;
    push dword SEL_LDT_DATA  ;SS
    push dword [fs:esi+8*4] ;ESP
    push dword [fs:esi+4*4] ;EFLAGS
    ;pushfd
    push dword SEL_LDT_CODE  ;CS
    push dword [fs:esi+9*4] ;EIP

    mov esi, [fs:esi+7*4]
    push eax
    mov al, 20h
    out 20h, al
    pop eax
iret