;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; RDOS operating system
; Copyright (C) 1988-2000, Leif Ekblad
;
; This program is free software; you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version. The only exception to this rule
; is for commercial usage in embedded systems. For information on
; usage in commercial embedded systems, contact embedded@rdos.net
;
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
;
; You should have received a copy of the GNU General Public License
; along with this program; if not, write to the Free Software
; Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
;
; The author of this program may be contacted at leif@rdos.net
;
; OS.ASM
; OS (kernel level) gate handling module
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
						
		NAME os

GateSize = 16

INCLUDE system.def
INCLUDE protseg.def
INCLUDE os.def
INCLUDE os.inc

gate_entry	STRUC
gate_name_sel		DW ?
gate_name_offset	DD ?
virt_gate_nr		DW ?
gate_entry	ENDS

code	SEGMENT byte public 'CODE'

	.386p

	extrn create_data_sel16:near
	extrn create_call_gate_sel16:near
	extrn create_int_gate_sel:near
	extrn create_tss_sel:near

	extrn allocate_fixed_system_mem:near

	assume cs:code

PAGE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			INIT_OSGATE
;
;		DESCRIPTION:	Init module
;
;						
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

	public init_osgate

init_osgate	PROC near
	push ds
	push es
	pusha
;
	mov ax,cs
	mov ds,ax
	mov si,OFFSET register_gate
	mov bx,os_begin_sel
	xor cl,cl
	push cs
	call create_call_gate_sel16
;
	mov bx,osgate_sel
	mov eax,osgate_entries SHL 3
	push cs
	call allocate_fixed_system_mem
	mov cx,osgate_entries SHL 3
	xor al,al
	xor di,di
	rep stosb
	xor di,di
	mov es:[di].gate_name_offset,OFFSET register_gate_name
	mov es:[di].gate_name_sel,cs
	mov cx,osgate_entries-1
	mov di,8
init_osgate_loop:
	mov es:[di].gate_name_offset,OFFSET illegal_gate_name
	mov es:[di].gate_name_sel,cs
	add di,8
	loop init_osgate_loop
;
	mov ax,cs
	mov ds,ax
	mov es,ax
	mov si,OFFSET is_valid_osgate
	mov di,OFFSET is_valid_osgate_name
	mov ax,is_valid_osgate_nr
	xor cl,cl
	RegisterOsGate
;
	popa
	pop es
	pop ds
	ret
init_osgate	ENDP

PAGE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			REGISTER_GATE
;
;		DESCRIPTION:	Register an OS gate
;
;		PARAMETERS:		AX		GATE NUMBER
;						CL		NUMBER OF 16-BIT PARAMETERS
;						DS:SI	GATE CALL ADDRESS
;						ES:DI	GATE NAME ADDRESS
;						
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

illegal_gate_name	DB 'Undefined Gate',0

illegal_gate	PROC far
	mov ax,1231h
	mov es,ax
	int 3
	ret
illegal_gate	ENDP

register_gate_name	DB 'Register Kernel Gate',0


register_gate	PROC far
	push ds
	push fs
	push gs
	push bx
;
	mov bx,ax
	shl bx,3
	add bx,os_begin_sel
	push cs
	call create_call_gate_sel16
;
	push ds
	mov bx,ax
	mov ax,osgate_sel
	mov ds,ax
	pop ax
	shl bx,3
	mov [bx].gate_name_sel,es
	xor eax,eax
	mov ax,di
	mov [bx].gate_name_offset,eax
;
	pop bx
	pop gs
	pop fs
	pop ds
	ret
register_gate	ENDP

PAGE

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
;
;		NAME:			IS_VALID_OSGATE
;
;		DESCRIPTION:	Check if a gate number is registered
;
;		PARAMETERS:		AX		GATE NUMBER
;						
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

is_valid_osgate_name	DB 'Is Valid Kernel Gate',0

is_valid_osgate	PROC far
	push ds
	push ax
	push bx
;
	mov bx,ax
	mov ax,osgate_sel
	mov ds,ax
	shl bx,3
	mov ax,[bx].gate_name_sel
	cmp ax,kernel_code
	clc
	jne is_valid_gate_done
;
	mov eax,[bx].gate_name_offset
	cmp eax,OFFSET illegal_gate_name
	clc
	jne is_valid_gate_done
;
	stc

is_valid_gate_done:
	pop bx
	pop ax
	pop ds
	ret
is_valid_osgate	ENDP

code	ENDS

	END