Import backup from 2004-03-12

This commit is contained in:
Josh Holtrop 2004-03-12 22:00:00 -05:00
parent 302538b8ff
commit ab576bf18f
27 changed files with 1839 additions and 139 deletions

View File

@ -90,7 +90,7 @@ File_Copy:
######################################
# Create and Format the floppy image #
######################################
install_img: $(FLOPPY_IMAGE)
install_img:
$(MKDOSFS_PROG) -C -F $(FLOPPY_FAT_SIZE) -r 112 $(FLOPPY_IMAGE) $(FLOPPY_BLOCK_COUNT)
############################################

107
Makefile.ms Normal file
View File

@ -0,0 +1,107 @@
#####################################################################
# Author: Benjamen R. Meyer #
# Date: 2004-2-15 #
# Purpose: To build Josh Holtrop's OS (HOS) using GNU make #
# Note: This makefile is for use on Linux & other Unix-like systems #
#####################################################################
##############
# Variables: #
##############
# Information for creating a floppy image
# Note: FLOPPY_FS and FLOPPY_FAT_SIZE are related fields.
# FLOPPY_FAT_SIZE should be either 12 or 16,
# depending on if FLOPPY_FS is FAT12 or FAT16, respectively.
MKDOSFS_PROG=format
FLOPPY_IMAGE=floppy.img
FLOPPY_BLOCK_COUNT=1440
FLOPPY_FS=FAT12
FLOPPY_FAT_SIZE=12
# Floppy images are good for programs like VMware and Bochs Pentium Emulator. ;-)
# Program for copying
COPY_BIN=copy
##########################################
# Build the IPL (Boot Loader) and Kernel #
##########################################
all:
# A word of warning to users :-> And a little helpful information ;-)
@echo "Installation must be done as root."
@echo "Type 'make install' to install to a floppy in drive '/dev/fd0'"
@echo "Type 'make install_img' to create a floppy image and install to it."
cd boot; make
cd kernel; make -f Makefile.ms
#################################################
# Clean up the source directory of any binaries #
#################################################
clean:
cd boot; make clean
cd kernel; make clean
###########################################
# The following is for the floppy drive #
# Note: This must be done on *nix as root #
###########################################
##########################
# Make install to floppy #
##########################
install: Install_IPL File_Copy
############################################
# Write the Stage 1 IPL to the boot sector #
############################################
Install_IPL:
$(MKDOSFS_PROG) a: /q /v:HOS
partcopy stage1.bin 0 200 -f0
#################################
# Copy the files onto the drive #
#################################
File_Copy:
@echo "Copying stage 2 bootloader to the floppy..."
$(COPY_BIN) boot/stage2.bin a:\
@echo "Copying kernel to the floppy..."
$(COPY_BIN) kernel/kernel.bin a:\
############################################
# The following is for the floppy image. #
# Note: This must be done on *nix as root. #
############################################
######################################
# Create and Format the floppy image #
######################################
install_img:
$(MKDOSFS_PROG) -C -F $(FLOPPY_FAT_SIZE) -r 112 $(FLOPPY_IMAGE) $(FLOPPY_BLOCK_COUNT)
############################################
# Write the Stage 1 IPL to the boot sector #
############################################
@echo "Writing boot sector to image..."
dd if=stage1.bin of=$(FLOPPY_IMAGE) seek=0
#################################
# Copy the files onto the image #
#################################
@echo "Mounting floppy image..."
mount $(FLOPPY_IMAGE) $(FLOPPY_MOUNT) -o loop
@echo "Copying stage 2 bootloader to the floppy image..."
$(COPY_BIN) stage2.bin $(FLOPPY_MOUNT)
@echo "Copying kernel to the floppy image..."
$(COPY_BIN) kernel.bin $(FLOPPY_MOUNT)
@echo "Unmounting floppy image..."
mount $(FLOPPY_IMAGE) $(FLOPPY_MOUNT) -o loop

View File

@ -27,4 +27,4 @@ stage2: stage2.asm
# Clean #
#########
clean:
rm *.bin
- rm *.bin

View File

@ -19,11 +19,11 @@ NASM_FLAGS=-f $(KERNEL_FORMAT)
# C Compile Information:
CC=gcc
CC_FLAGS=-ffreestanding -fno-builtin -nostdlib -nodefaultlibs -I. -Wall
CC_FLAGS=-ffreestanding -fleading-underscore -fno-builtin -nostdlib -nodefaultlibs -I. -Wall
# Linker Information:
LD=ld
LD_FLAGS=-nodefaultlibs -nostdlib -T link.ld
LD_FLAGS=-nodefaultlibs -nostdlib --no-demangle -T link.ld
###############################

View File

@ -1,71 +1,74 @@
#####################################################################
# Author: Benjamen R. Meyer #
# Date: 2004-2-15 #
# Purpose: To build Josh Holtrop's OS (HOS) using GNU make #
# Note: This makefile is for use on Linux & other Unix-like systems #
#####################################################################
##############
# Variables: #
##############
# Format of kernel object files:
# Do not change unless you know what you are doing
KERNEL_FORMAT=aout
# Assembler information:
NASM_BIN=nasm
NASM_FLAGS=-f $(KERNEL_FORMAT)
# C Compile Information:
CC=gcc
CC_FLAGS=-ffreestanding -fno-builtin -nostdlib -nodefaultlibs -I. -Wall
# Linker Information:
LD=ld
LD_FLAGS=-nodefaultlibs -nostdlib -T link.ld
###############################
# Linking the kernel together #
###############################
all: Asm_Kernel Asm_Functions C_Kernel
$(LD) $(LD_FLAGS) -o kernel.bin -Map ./lst/LDout.doc ks.o kernel.o asmfuncs.o fdc.o functions.o keyboard.o kio.o mm.o mouse.o stdfont.o video.o vmm.o
##########################
# Assembly Kernel Loader #
##########################
Asm_Kernel: kernel.asm
$(NASM_BIN) $(NASM_FLAGS) -o ks.o -l ./lst/kernel.lst kernel.asm
#################################
# Assembly Functions for Kernel #
#################################
Asm_Functions: asmfuncs.asm
$(NASM_BIN) $(NASM_FLAGS) -o asmfuncs.o -l ./lst/asmfuncs.lst asmfuncs.asm
############
# C Kernel #
############
C_Kernel:
$(CC) $(CC_FLAGS) -c kernel.c -o kernel.o
$(CC) $(CC_FLAGS) -c kio.c -o kio.o
$(CC) $(CC_FLAGS) -c functions.c -o functions.o
$(CC) $(CC_FLAGS) -c sys/rtc.c -o rtc.o
$(CC) $(CC_FLAGS) -c sys/pic.c -o pic.o
$(CC) $(CC_FLAGS) -c sys/io.c -o io.o
$(CC) $(CC_FLAGS) -c sys/cmos.c -o cmos.o
$(CC) $(CC_FLAGS) -c string/string.c -o string.o
$(CC) $(CC_FLAGS) -c video/stdfont.c -o stdfont.o
$(CC) $(CC_FLAGS) -c video/video.c -o video.o
$(CC) $(CC_FLAGS) -c block/fdc.c -o fdc.o
$(CC) $(CC_FLAGS) -c char/keyboard.c -o keyboard.o
$(CC) $(CC_FLAGS) -c char/mouse.c -o mouse.o
$(CC) $(CC_FLAGS) -c mm/mm.c -o mm.o
$(CC) $(CC_FLAGS) -c mm/vmm.c -o vmm.o
#################################################
# Clean up the source directory of any binaries #
#################################################
clean:
- rm *.o ./lst/*.lst ./lst/*.doc *.bin
#####################################################################
# Author: Benjamen R. Meyer #
# Date: 2004-2-15 #
# Purpose: To build Josh Holtrop's OS (HOS) using GNU make #
# Note: This makefile is for use on Linux & other Unix-like systems #
#####################################################################
##############
# Variables: #
##############
# Format of kernel object files:
# Do not change unless you know what you are doing
KERNEL_FORMAT=coff
# Assembler information:
NASM_BIN=nasm
NASM_FLAGS=-f $(KERNEL_FORMAT)
# C Compile Information:
CC=gcc
CC_FLAGS=-ffreestanding -fno-builtin -nostdlib -nodefaultlibs -I. -Wall
# Linker Information:
LD=ld
LD_FLAGS=-nodefaultlibs -nostdlib --no-demangle -T link.ms
###############################
# Linking the kernel together #
###############################
all: Asm_Kernel Asm_Functions C_Kernel
$(LD) $(LD_FLAGS) -o kernel.pe -Map ./lst/LDout.doc ks.o kernel.o asmfuncs.o fdc.o functions.o keyboard.o kio.o mm.o mouse.o stdfont.o video.o vmm.o rtc.o pic.o io.o string.o cmos.o
objcopy -O binary kernel.pe kernel.bin
- rm kernel.pe
##########################
# Assembly Kernel Loader #
##########################
Asm_Kernel: kernel.asm
$(NASM_BIN) $(NASM_FLAGS) -o ks.o -l ./lst/kernel.lst kernel.asm
#################################
# Assembly Functions for Kernel #
#################################
Asm_Functions: asmfuncs.asm
$(NASM_BIN) $(NASM_FLAGS) -o asmfuncs.o -l ./lst/asmfuncs.lst asmfuncs.asm
############
# C Kernel #
############
C_Kernel:
$(CC) $(CC_FLAGS) -c kernel.c -o kernel.o
$(CC) $(CC_FLAGS) -c kio.c -o kio.o
$(CC) $(CC_FLAGS) -c functions.c -o functions.o
$(CC) $(CC_FLAGS) -c sys/rtc.c -o rtc.o
$(CC) $(CC_FLAGS) -c sys/pic.c -o pic.o
$(CC) $(CC_FLAGS) -c sys/io.c -o io.o
$(CC) $(CC_FLAGS) -c sys/cmos.c -o cmos.o
$(CC) $(CC_FLAGS) -c string/string.c -o string.o
$(CC) $(CC_FLAGS) -c video/stdfont.c -o stdfont.o
$(CC) $(CC_FLAGS) -c video/video.c -o video.o
$(CC) $(CC_FLAGS) -c block/fdc.c -o fdc.o
$(CC) $(CC_FLAGS) -c char/keyboard.c -o keyboard.o
$(CC) $(CC_FLAGS) -c char/mouse.c -o mouse.o
$(CC) $(CC_FLAGS) -c mm/mm.c -o mm.o
$(CC) $(CC_FLAGS) -c mm/vmm.c -o vmm.o
#################################################
# Clean up the source directory of any binaries #
#################################################
clean:
- rm *.o ./lst/*.lst ./lst/*.doc *.bin

View File

@ -3,11 +3,11 @@
; Created: 10/23/03
; Modified: 02/26/04
[extern putc]
[extern console_memory]
[extern cursorPosition]
[extern video_drawConsole]
[extern videoMode]
[extern _putc]
[extern _console_memory]
[extern _cursorPosition]
[extern _video_drawConsole]
[extern _videoMode]
%macro jzfar 1
jnz %%skip
@ -18,8 +18,8 @@
;stores the parameter to the CR0 register
;extern dword write_cr0(dword cr0);
[global write_cr0]
write_cr0:
[global _write_cr0]
_write_cr0:
push ebp
mov ebp, esp
mov eax, [ebp+8]
@ -29,15 +29,15 @@ write_cr0:
;returns the value in the CR0 register
;extern dword read_cr0();
[global read_cr0]
read_cr0:
[global _read_cr0]
_read_cr0:
mov eax, cr0;
ret
;stores the parameter to the CR3 register
;extern dword write_cr3(dword cr3);
[global write_cr3]
write_cr3:
[global _write_cr3]
_write_cr3:
push ebp
mov ebp, esp
mov eax, [ebp+8]
@ -48,8 +48,8 @@ write_cr3:
;returns the value in the CR2 register
;extern dword read_cr2();
[global read_cr2]
read_cr2:
[global _read_cr2]
_read_cr2:
mov eax, cr2;
ret
@ -57,8 +57,8 @@ read_cr2:
;returns the value in the CR3 register
;extern dword read_cr3();
[global read_cr3]
read_cr3:
[global _read_cr3]
_read_cr3:
mov eax, cr3;
ret
@ -66,8 +66,8 @@ read_cr3:
;compares one string to another
;returns 0 if the strings are different
;extern dword strcmp(char *str1, char *str2);
[global strcmp]
strcmp:
[global _strcmp]
_strcmp:
push ebp
mov ebp, esp
push esi
@ -98,8 +98,8 @@ strcmp_done:
;copies a string from the source to the destination parameter
;extern void strcpy(char *dest, char *src);
[global strcpy]
strcpy:
[global _strcpy]
_strcpy:
push ebp
mov ebp, esp
push esi
@ -118,8 +118,8 @@ strcpyloop:
;copies memory of n bytes from src to destination
;void memcpy(void *dest, void *src, dword n);
[global memcpy]
memcpy:
[global _memcpy]
_memcpy:
push ebp
mov ebp, esp
push esi
@ -141,8 +141,8 @@ memcpy:
;copies memory of n dwords (n*4 bytes) from src to destination
;void memcpyd(void *dest, void *src, dword n);
[global memcpyd]
memcpyd:
[global _memcpyd]
_memcpyd:
push ebp
mov ebp, esp
push esi
@ -164,8 +164,8 @@ memcpyd:
;sets num bytes at buffer to the value of c
;void *memset(void *buffer, int c, int num);
[global memset]
memset:
[global _memset]
_memset:
push ebp
mov ebp, esp
push edi
@ -186,8 +186,8 @@ memset:
;sets num words at buffer to the value of c
;void *memsetw(void *buffer, int c, int num);
[global memsetw]
memsetw:
[global _memsetw]
_memsetw:
push ebp
mov ebp, esp
push edi
@ -208,8 +208,8 @@ memsetw:
;sets num dwords at buffer to the value of c
;void *memsetd(void *buffer, int c, int num);
[global memsetd]
memsetd:
[global _memsetd]
_memsetd:
push ebp
mov ebp, esp
push edi
@ -230,8 +230,8 @@ memsetd:
;returns the number of characters in a string
;extern dword strlen(char *str);
[global strlen]
strlen:
[global _strlen]
_strlen:
push ebp
mov ebp, esp
push esi
@ -254,8 +254,8 @@ strlendone:
;this function invalidates the page directory/table entry that
; would be used to access the memory address given in the parameter
;extern void invlpg_(dword addr);
[global invlpg_]
invlpg_:
[global _invlpg_]
_invlpg_:
mov eax, [esp+4]
invlpg [eax]
ret
@ -264,8 +264,8 @@ invlpg_:
;
;void writeCursorPosition(word pos)
;
[global writeCursorPosition]
writeCursorPosition:
[global _writeCursorPosition]
_writeCursorPosition:
push ebp
mov ebp, esp
@ -303,8 +303,8 @@ writeCursorPosition:
;
;word getCursorPosition()
;
[global getCursorPosition]
getCursorPosition:
[global _getCursorPosition]
_getCursorPosition:
push ebx
push edx
@ -334,8 +334,8 @@ getCursorPosition:
;
;int puts(char *str)
;
[global puts]
puts:
[global _puts]
_puts:
push ebp
mov ebp, esp
push esi
@ -346,7 +346,7 @@ puts_loop:
cmp al, 0
jz puts_done
push eax
call putc
call _putc
add esp, 4
jmp puts_loop
@ -359,8 +359,8 @@ puts_done:
[global putDecu]
putDecu:
[global _putDecu]
_putDecu:
push ebp
mov ebp, esp
sub esp, 24
@ -413,7 +413,7 @@ L7:
mov eax, 0
mov al, BYTE [ebp-5]
push eax
call putc
call _putc
add esp, 16
jmp L5
L6:
@ -422,7 +422,7 @@ L6:
add eax, 48
and eax, 255
push eax
call putc
call _putc
add esp, 16
leave
ret
@ -430,8 +430,8 @@ L6:
[global putDec]
putDec:
[global _putDec]
_putDec:
push ebp
mov ebp, esp
sub esp, 24
@ -439,7 +439,7 @@ putDec:
jns L9
sub esp, 12
push 45
call putc
call _putc
add esp, 16
neg DWORD [ebp+8]
L9:
@ -493,7 +493,7 @@ L15:
mov eax, 0
mov al, BYTE [ebp-5]
push eax
call putc
call _putc
add esp, 16
jmp L13
L14:
@ -502,7 +502,7 @@ L14:
add eax, 48
and eax, 255
push eax
call putc
call _putc
add esp, 16
leave
ret

7
kernel/block/rd.c Normal file
View File

@ -0,0 +1,7 @@
// rd.c
// Author: Josh Holtrop
// Date: 03/11/04
#include "rd.h"

12
kernel/block/rd.h Normal file
View File

@ -0,0 +1,12 @@
// rd.h
// Author: Josh Holtrop
// Date: 03/11/04
#ifndef __HOS_RD__
#define __HOS_RD__ __HOS_RD__
#endif

7
kernel/fs/fat12.c Normal file
View File

@ -0,0 +1,7 @@
// fat12.c
// Author: Josh Holtrop
// Date: 03/11/04
#include "fat12.h"

12
kernel/fs/fat12.h Normal file
View File

@ -0,0 +1,12 @@
// fat12.h
// Author: Josh Holtrop
// Date: 03/11/04
#ifndef __HOS_FAT12__
#define __HOS_FAT12__ __HOS_FAT12__
#endif

33
kernel/fs/vfs.c Normal file
View File

@ -0,0 +1,33 @@
// vfs.c
// Author: Josh Holtrop
// Date: 03/11/04
#include "vfs.h"
#include "hos_defines.h"
Volume *firstVolume = 0;
void vfs_init()
{
dword initrdLoaded = *(byte *)BOOT_HASRD;
}
Volume *vfs_newVolume()
{
Volume *vol = malloc(sizeof(Volume));
vfs_getLastVolume()->link = vol;
return vol;
}
Volume *vfs_getLastVolume()
{
Volume *vol = firstVolume;
while (vol)
vol = vol->link;
return vol;
}

26
kernel/fs/vfs.h Normal file
View File

@ -0,0 +1,26 @@
// vfs.h
// Author: Josh Holtrop
// Date: 03/11/04
#ifndef __HOS_VFS__
#define __HOS_VFS__ __HOS_VFS__
#include "hos_defines.h"
typedef struct
{
void *diskDevice;
int deviceType;
char label[12];
void *link;
} Volume;
enum VFS_DISK_TYPES {VFS_RD, VFS_FD, VFS_HD, VFS_NUM_DISK_TYPES};
void vfs_init();
Volume *vfs_newVolume();
Volume *vfs_getLastVolume();
#endif

View File

@ -18,7 +18,7 @@
#define BOOT_FIRST_MEMMAP 0xC0092000
#define BOOT_VIDEO_MODE 0xC0090002
#define BOOT_VIDEO_MODE_INFO_BLOCK 0xC0090306
#define BOOT_HASRD 0xC0090000
typedef unsigned char byte;
typedef unsigned short word;

View File

@ -76,7 +76,7 @@ isr_main:
push eax
call isr
call _isr
add esp, 4
@ -98,7 +98,7 @@ sc1:
cmp eax, 1 ;syscall 1 - putc
jnz sc2
push ebx
call putc
call _putc
add esp, 4
jmp scdone
sc2:

View File

@ -14,10 +14,10 @@
%define KERNEL_P 0x106000 ;1mb+24kb - the kernel's physical address
%define KERNEL_V KERNEL_P+0xC0000000 ;3gb+1mb+24kb, the virtual address of the kernel
[global start]
[extern isr]
[extern k_init]
[extern putc]
[global _start]
[extern _isr]
[extern _k_init]
[extern _putc]
bits 32
@ -26,7 +26,7 @@ bits 32
;We must enable paging with the first 4mb mapped 1:1 virtual:physical
; and with the 4mb starting at 0xC000_0000 mapped to the first 4mb physical.
;Then we can start using our "real" gdt, then unmap the lower 4mb.
start:
_start:
cli ;if they weren't already off
xor eax, eax
@ -91,7 +91,7 @@ newgdtcontinue:
mov esp, 0xc0200000 ;stack just under 3gb+2mb, moves downward
lidt [idtr] ;load idt
call k_init
call _k_init
haltit:
hlt ;halt processor when k_init is done
jmp haltit ;shouldn't get here...

View File

@ -47,7 +47,7 @@ void k_init()
{
int p = video_getWidth()*video_getHeight()-1;
for (; p >= 0; p--)
video_pseti(p, 0x00000066);
video_pseti(p, 0x00000075);
kio_drawConsole();
}

View File

@ -139,8 +139,8 @@ void putHex(dword number)
void kio_putBCD(dword bcd)
{
putDecu((bcd & 0xF) >> 4);
putDecu(bcd & 0xF);
putc(((bcd & 0xF) >> 4) + '0');
putc((bcd & 0xF) + '0');
}

13
kernel/ld.exe.stackdump Normal file
View File

@ -0,0 +1,13 @@
Exception: STATUS_ACCESS_VIOLATION at eip=00462A1A
eax=00000000 ebx=00001000 ecx=0A04865C edx=00000200 esi=0A04724C edi=FFFFFE00
ebp=0022EB88 esp=0022EB20 program=C:\cygwin\bin\ld.exe
cs=001B ds=0023 es=0023 fs=0038 gs=0000 ss=0023
Stack trace:
Frame Function Args
0022EB88 00462A1A (0A046CD8, 0022EC70, 0A04EF10, 610D3F69)
0022EE68 00448567 (0A046CD8, 30303030, 0022EE00, 0A04E758)
0022EEF8 0043FCA0 (0A046CD8, 0041BBDE, 77E88AC8, FFFFFFFF)
0022EF40 0041C322 (0000001B, 6167241C, 0A0400A8, 0022EF98)
0022EF80 61005DE0 (0022EF98, 00000000, 002304CC, 536CD652)
0022FF90 61005EE5 (00000000, 00000000, 00000000, 00000000)
End of stack trace

View File

@ -1,5 +1,5 @@
OUTPUT_FORMAT("binary")
ENTRY(start)
ENTRY(_start)
SECTIONS
{
.text 0xC0106000 : {

22
kernel/link.ms Normal file
View File

@ -0,0 +1,22 @@
OUTPUT_FORMAT("pe-i386")
ENTRY(_start)
SECTIONS
{
.text 0xC0106000 : {
code = .; _code = .; __code = .;
*(.text)
. = ALIGN(4096);
}
.data : {
data = .; _data = .; __data = .;
*(.data)
. = ALIGN(4096);
}
.bss :
{
bss = .; _bss = .; __bss = .;
*(.bss)
. = ALIGN(4096);
}
end = .; _end = .; __end = .;
}

View File

@ -5,11 +5,11 @@
// These functions are for reading and writing various values stored on the CMOS Real Time Clock
// Parameters / return values are in BCD format
#include "hos_defines.h"
#ifndef __HOS_RTC__
#define __HOS_RTC__ __HOS_RTC__
#include "hos_defines.h"
unsigned char rtc_readDay();
unsigned char rtc_readMonth();
unsigned char rtc_readYear();

510
nous/asmfuncs.asm Normal file
View File

@ -0,0 +1,510 @@
; asmfuncs.asm
; Josh Holtrop
; Created: 10/23/03
; Modified: 02/26/04
[extern putc]
[extern console_memory]
[extern cursorPosition]
[extern video_drawConsole]
[extern videoMode]
%macro jzfar 1
jnz %%skip
jmp %1
%%skip:
%endmacro
;stores the parameter to the CR0 register
;extern dword write_cr0(dword cr0);
[global write_cr0]
write_cr0:
push ebp
mov ebp, esp
mov eax, [ebp+8]
mov cr0, eax
pop ebp
ret
;returns the value in the CR0 register
;extern dword read_cr0();
[global read_cr0]
read_cr0:
mov eax, cr0;
ret
;stores the parameter to the CR3 register
;extern dword write_cr3(dword cr3);
[global write_cr3]
write_cr3:
push ebp
mov ebp, esp
mov eax, [ebp+8]
mov cr3, eax
pop ebp
ret
;returns the value in the CR2 register
;extern dword read_cr2();
[global read_cr2]
read_cr2:
mov eax, cr2;
ret
;returns the value in the CR3 register
;extern dword read_cr3();
[global read_cr3]
read_cr3:
mov eax, cr3;
ret
;compares one string to another
;returns 0 if the strings are different
;extern dword strcmp(char *str1, char *str2);
[global strcmp]
strcmp:
push ebp
mov ebp, esp
push esi
push edi
mov esi, [ebp+8]
mov edi, [ebp+12]
strcmp_loop1:
lodsb
mov ah, [edi]
inc edi
cmp ah, al
jnz strcmp_ne
or al, al
jz strcmp_e
jmp strcmp_loop1
strcmp_e:
mov eax, 1
jmp short strcmp_done
strcmp_ne:
xor eax, eax
strcmp_done:
pop edi
pop esi
pop ebp
ret
;copies a string from the source to the destination parameter
;extern void strcpy(char *dest, char *src);
[global strcpy]
strcpy:
push ebp
mov ebp, esp
push esi
push edi
mov edi, [ebp+8]
mov esi, [ebp+12]
strcpyloop:
lodsb
stosb
or al, al
jnz strcpyloop
pop edi
pop esi
pop ebp
ret
;copies memory of n bytes from src to destination
;void memcpy(void *dest, void *src, dword n);
[global memcpy]
memcpy:
push ebp
mov ebp, esp
push esi
push edi
push ecx
mov edi, [ebp+8]
mov esi, [ebp+12]
mov ecx, [ebp+16]
cld
rep movsb
pop ecx
pop edi
pop esi
pop ebp
ret
;copies memory of n dwords (n*4 bytes) from src to destination
;void memcpyd(void *dest, void *src, dword n);
[global memcpyd]
memcpyd:
push ebp
mov ebp, esp
push esi
push edi
push ecx
mov edi, [ebp+8]
mov esi, [ebp+12]
mov ecx, [ebp+16]
cld
rep movsd
pop ecx
pop edi
pop esi
pop ebp
ret
;sets num bytes at buffer to the value of c
;void *memset(void *buffer, int c, int num);
[global memset]
memset:
push ebp
mov ebp, esp
push edi
push ecx
mov edi, [ebp+8]
push edi ;save for return address
mov eax, [ebp+12]
mov ecx, [ebp+16]
rep stosb
pop eax
pop ecx
pop edi
pop ebp
ret
;sets num words at buffer to the value of c
;void *memsetw(void *buffer, int c, int num);
[global memsetw]
memsetw:
push ebp
mov ebp, esp
push edi
push ecx
mov edi, [ebp+8]
push edi ;save for return address
mov eax, [ebp+12]
mov ecx, [ebp+16]
rep stosw
pop eax
pop ecx
pop edi
pop ebp
ret
;sets num dwords at buffer to the value of c
;void *memsetd(void *buffer, int c, int num);
[global memsetd]
memsetd:
push ebp
mov ebp, esp
push edi
push ecx
mov edi, [ebp+8]
push edi ;save for return address
mov eax, [ebp+12]
mov ecx, [ebp+16]
rep stosd
pop eax
pop ecx
pop edi
pop ebp
ret
;returns the number of characters in a string
;extern dword strlen(char *str);
[global strlen]
strlen:
push ebp
mov ebp, esp
push esi
push ebx
mov esi, [ebp+8]
xor ebx, ebx
strlenloop:
lodsb
or al, al
jz strlendone
inc ebx
jmp strlenloop
strlendone:
mov eax, ebx
pop ebx
pop esi
pop ebp
ret
;this function invalidates the page directory/table entry that
; would be used to access the memory address given in the parameter
;extern void invlpg_(dword addr);
[global invlpg_]
invlpg_:
mov eax, [esp+4]
invlpg [eax]
ret
;
;void writeCursorPosition(word pos)
;
[global writeCursorPosition]
writeCursorPosition:
push ebp
mov ebp, esp
push eax
push ebx
push edx
mov eax, [ebp+8] ;cursor position in ax
mov bl, al
mov dx, 0x03D4
mov al, 0x0E
out dx, al
inc dx
mov al, ah
out dx, al
dec dx
mov al, 0x0F
out dx, al
inc dx
mov al, bl
out dx, al
pop edx
pop ebx
pop eax
pop ebp
ret
;
;word getCursorPosition()
;
[global getCursorPosition]
getCursorPosition:
push ebx
push edx
xor eax, eax
mov dx, 0x03D4
mov al, 0x0E
out dx, al
inc dx
in al, dx
mov bl, al
dec dx
mov al, 0x0F
out dx, al
inc dx
in al, dx
mov ah, bl
pop edx
pop ebx
ret
;
;int puts(char *str)
;
[global puts]
puts:
push ebp
mov ebp, esp
push esi
push eax
mov esi, [ebp+8] ;esi = to string
puts_loop:
lodsb
cmp al, 0
jz puts_done
push eax
call putc
add esp, 4
jmp puts_loop
puts_done:
pop eax
pop esi
pop ebp
ret
[global putDecu]
putDecu:
push ebp
mov ebp, esp
sub esp, 24
mov DWORD [ebp-4], 1
mov BYTE [ebp-5], 0
L2:
mov edx, DWORD [ebp+8]
mov eax, -858993459
mul edx
mov eax, edx
shr eax, 3
cmp eax, DWORD [ebp-4]
jae L4
jmp L3
L4:
mov eax, DWORD [ebp-4]
mov edx, eax
sal edx, 2
add edx, eax
lea eax, [edx+edx]
mov DWORD [ebp-4], eax
jmp L2
L3:
nop
L5:
cmp DWORD [ebp-4], 1
ja L7
jmp L6
L7:
mov edx, DWORD [ebp+8]
mov eax, edx
mov edx, 0
div DWORD [ebp-4]
mov DWORD [ebp-12], eax
mov al, BYTE [ebp-12]
mov BYTE [ebp-5], al
mov eax, 0
mov al, BYTE [ebp-5]
imul eax, DWORD [ebp-4]
sub DWORD [ebp+8], eax
mov edx, DWORD [ebp-4]
mov eax, -858993459
mul edx
mov eax, edx
shr eax, 3
mov DWORD [ebp-4], eax
lea eax, [ebp-5]
add BYTE [eax], 48
sub esp, 12
mov eax, 0
mov al, BYTE [ebp-5]
push eax
call putc
add esp, 16
jmp L5
L6:
sub esp, 12
mov al, BYTE [ebp+8]
add eax, 48
and eax, 255
push eax
call putc
add esp, 16
leave
ret
[global putDec]
putDec:
push ebp
mov ebp, esp
sub esp, 24
cmp DWORD [ebp+8], 0
jns L9
sub esp, 12
push 45
call putc
add esp, 16
neg DWORD [ebp+8]
L9:
mov DWORD [ebp-4], 1
mov BYTE [ebp-5], 0
L10:
mov eax, DWORD [ebp+8]
cmp eax, DWORD [ebp-4]
jae L12
jmp L11
L12:
mov eax, DWORD [ebp-4]
mov edx, eax
sal edx, 2
add edx, eax
lea eax, [edx+edx]
mov DWORD [ebp-4], eax
jmp L10
L11:
mov edx, DWORD [ebp-4]
mov eax, -858993459
mul edx
mov eax, edx
shr eax, 3
mov DWORD [ebp-4], eax
L13:
cmp DWORD [ebp-4], 1
ja L15
jmp L14
L15:
mov edx, DWORD [ebp+8]
mov eax, edx
mov edx, 0
div DWORD [ebp-4]
mov DWORD [ebp-12], eax
mov al, BYTE [ebp-12]
mov BYTE [ebp-5], al
mov eax, 0
mov al, BYTE [ebp-5]
imul eax, DWORD [ebp-4]
sub DWORD [ebp+8], eax
mov edx, DWORD [ebp-4]
mov eax, -858993459
mul edx
mov eax, edx
shr eax, 3
mov DWORD [ebp-4], eax
lea eax, [ebp-5]
add BYTE [eax], 48
sub esp, 12
mov eax, 0
mov al, BYTE [ebp-5]
push eax
call putc
add esp, 16
jmp L13
L14:
sub esp, 12
mov al, BYTE [ebp+8]
add eax, 48
and eax, 255
push eax
call putc
add esp, 16
leave
ret

117
nous/idt.inc Normal file
View File

@ -0,0 +1,117 @@
;idt.inc
;Author: Josh Holtrop
;Date: 10/30/03
;Modified: 03/02/04
idtr:
dw 50*8-1 ;size of idt
dd IDT_V ;address of idt
%macro isr_label 1
isr_%1:
push eax
mov eax, %1
jmp isr_main
%endmacro
isr_label 0
isr_label 1
isr_label 2
isr_label 3
isr_label 4
isr_label 5
isr_label 6
isr_label 7
isr_label 8
isr_label 9
isr_label 10
isr_label 11
isr_label 12
isr_label 13
isr_label 14
isr_label 15
isr_label 16
isr_label 17
isr_label 18
isr_label 19
isr_label 20
isr_label 21
isr_label 22
isr_label 23
isr_label 24
isr_label 25
isr_label 26
isr_label 27
isr_label 28
isr_label 29
isr_label 30
isr_label 31
isr_label 32
isr_label 33
isr_label 34
isr_label 35
isr_label 36
isr_label 37
isr_label 38
isr_label 39
isr_label 40
isr_label 41
isr_label 42
isr_label 43
isr_label 44
isr_label 45
isr_label 46
isr_label 47
isr_label 48
isr_label 49
isr_main:
cmp eax, 0x30
jz isr_syscall
pusha
push ds
push es
push eax
call isr
add esp, 4
pop es
pop ds
popa
pop eax
iret
isr_syscall:
pop eax ;syscall function number
pusha
push ds
push es
sc1:
cmp eax, 1 ;syscall 1 - putc
jnz sc2
push ebx
call putc
add esp, 4
jmp scdone
sc2:
scdone:
pop es
pop ds
popa
iret

102
nous/kernel.asm Normal file
View File

@ -0,0 +1,102 @@
;kernel.asm
;Author: Josh Holtrop
;Date: 10/30/03
;Modified: 10/30/03
%define GDT_P 0x100000; ;1mb physical - Global Descriptor Table space
%define GDT_V GDT_P+0xC0000000
%define IDT_P 0x102000 ;1mb+8kb - Interrupt Descriptor Table space
%define IDT_V IDT_P+0xC0000000
%define PDBR_P 0x104000 ;1mb+16kb - Page Directory Base Register (first PD)
%define PDBR_V PDBR_P+0xC0000000
%define LOPT_P 0x105000 ;1mb+20kb - LOw Page Table for mapping first 4mb
%define LOPT_V LOPT_P+0xC0000000
%define KERNEL_P 0x106000 ;1mb+24kb - the kernel's physical address
%define KERNEL_V KERNEL_P+0xC0000000 ;3gb+1mb+24kb, the virtual address of the kernel
[global start]
[extern isr]
[extern k_init]
[extern putc]
bits 32
;This is where the kernel begins execution
;At this point, the temporary gdt is set up to "map" 0xC000_0000 to 0x0.
;We must enable paging with the first 4mb mapped 1:1 virtual:physical
; and with the 4mb starting at 0xC000_0000 mapped to the first 4mb physical.
;Then we can start using our "real" gdt, then unmap the lower 4mb.
start:
cli ;if they weren't already off
xor eax, eax
mov edi, PDBR_V
mov ecx, 1024 ;clear the PDBR
rep stosd
mov [PDBR_V], dword LOPT_P|0x03 ;store the physical address of the LOw Page Table (read/write, present)
mov [PDBR_V+0xC00], dword LOPT_P|0x03 ;store the physical address of the LOw Page Table (read/write, present)
mov edi, LOPT_V
mov ecx, 1024
mov eax, 0x03 ;starting physical address = 0x0 (read/write, present flags)
fill_lopt_loop: ;fill the page table
stosd
add eax, 4096 ;increment next phsyical address by 4kb
loop fill_lopt_loop
mov eax, PDBR_P
mov cr3, eax ;store the Page Directory Base Address
mov eax, cr0
or eax, 0x80000000 ;set page enable bit
mov cr0, eax ;now paging is active!
mov edi, GDT_V
mov esi, gdt
mov ecx, gdt_end-gdt
copy_gdt:
lodsb
stosb
loop copy_gdt
mov edi, IDT_V ;destination
mov esi, isr_0 ;address of isr0
mov edx, isr_1-isr_0 ;distance between isr labels
mov ecx, 50 ;number of isrlabels
fill_idt:
mov ebx, esi
mov ax, si
stosw ;0 offset 15:0
mov ax, KERNEL_CODE
stosw ;2 selector 15:0
mov ax, 0x8E00
stosw ;4 [P][DPL][0][TYPE][0][0][0][0][0][0][0][0]
shr esi, 16
mov ax, si
stosw ;6 offset 31:16
mov esi, ebx
add esi, edx
loop fill_idt
mov word [IDT_V+0x30*8+4], 0xEE00 ;interrupt 0x30 has user priviledges
lgdt [gdtr] ;load gdt
jmp KERNEL_CODE:newgdtcontinue
newgdtcontinue:
mov ax, KERNEL_DATA
mov es, ax
mov ds, ax
mov gs, ax
mov fs, ax
mov ss, ax
mov esp, 0xc0200000 ;stack just under 3gb+2mb, moves downward
lidt [idtr] ;load idt
call k_init
haltit:
hlt ;halt processor when k_init is done
jmp haltit ;shouldn't get here...
%include "gdt.inc"
%include "idt.inc"

510
us/asmfuncs.asm Normal file
View File

@ -0,0 +1,510 @@
; asmfuncs.asm
; Josh Holtrop
; Created: 10/23/03
; Modified: 02/26/04
[extern _putc]
[extern _console_memory]
[extern _cursorPosition]
[extern _video_drawConsole]
[extern _videoMode]
%macro jzfar 1
jnz %%skip
jmp %1
%%skip:
%endmacro
;stores the parameter to the CR0 register
;extern dword write_cr0(dword cr0);
[global _write_cr0]
_write_cr0:
push ebp
mov ebp, esp
mov eax, [ebp+8]
mov cr0, eax
pop ebp
ret
;returns the value in the CR0 register
;extern dword read_cr0();
[global _read_cr0]
_read_cr0:
mov eax, cr0;
ret
;stores the parameter to the CR3 register
;extern dword write_cr3(dword cr3);
[global _write_cr3]
_write_cr3:
push ebp
mov ebp, esp
mov eax, [ebp+8]
mov cr3, eax
pop ebp
ret
;returns the value in the CR2 register
;extern dword read_cr2();
[global _read_cr2]
_read_cr2:
mov eax, cr2;
ret
;returns the value in the CR3 register
;extern dword read_cr3();
[global _read_cr3]
_read_cr3:
mov eax, cr3;
ret
;compares one string to another
;returns 0 if the strings are different
;extern dword strcmp(char *str1, char *str2);
[global _strcmp]
_strcmp:
push ebp
mov ebp, esp
push esi
push edi
mov esi, [ebp+8]
mov edi, [ebp+12]
strcmp_loop1:
lodsb
mov ah, [edi]
inc edi
cmp ah, al
jnz strcmp_ne
or al, al
jz strcmp_e
jmp strcmp_loop1
strcmp_e:
mov eax, 1
jmp short strcmp_done
strcmp_ne:
xor eax, eax
strcmp_done:
pop edi
pop esi
pop ebp
ret
;copies a string from the source to the destination parameter
;extern void strcpy(char *dest, char *src);
[global _strcpy]
_strcpy:
push ebp
mov ebp, esp
push esi
push edi
mov edi, [ebp+8]
mov esi, [ebp+12]
strcpyloop:
lodsb
stosb
or al, al
jnz strcpyloop
pop edi
pop esi
pop ebp
ret
;copies memory of n bytes from src to destination
;void memcpy(void *dest, void *src, dword n);
[global _memcpy]
_memcpy:
push ebp
mov ebp, esp
push esi
push edi
push ecx
mov edi, [ebp+8]
mov esi, [ebp+12]
mov ecx, [ebp+16]
cld
rep movsb
pop ecx
pop edi
pop esi
pop ebp
ret
;copies memory of n dwords (n*4 bytes) from src to destination
;void memcpyd(void *dest, void *src, dword n);
[global _memcpyd]
_memcpyd:
push ebp
mov ebp, esp
push esi
push edi
push ecx
mov edi, [ebp+8]
mov esi, [ebp+12]
mov ecx, [ebp+16]
cld
rep movsd
pop ecx
pop edi
pop esi
pop ebp
ret
;sets num bytes at buffer to the value of c
;void *memset(void *buffer, int c, int num);
[global _memset]
_memset:
push ebp
mov ebp, esp
push edi
push ecx
mov edi, [ebp+8]
push edi ;save for return address
mov eax, [ebp+12]
mov ecx, [ebp+16]
rep stosb
pop eax
pop ecx
pop edi
pop ebp
ret
;sets num words at buffer to the value of c
;void *memsetw(void *buffer, int c, int num);
[global _memsetw]
_memsetw:
push ebp
mov ebp, esp
push edi
push ecx
mov edi, [ebp+8]
push edi ;save for return address
mov eax, [ebp+12]
mov ecx, [ebp+16]
rep stosw
pop eax
pop ecx
pop edi
pop ebp
ret
;sets num dwords at buffer to the value of c
;void *memsetd(void *buffer, int c, int num);
[global _memsetd]
_memsetd:
push ebp
mov ebp, esp
push edi
push ecx
mov edi, [ebp+8]
push edi ;save for return address
mov eax, [ebp+12]
mov ecx, [ebp+16]
rep stosd
pop eax
pop ecx
pop edi
pop ebp
ret
;returns the number of characters in a string
;extern dword strlen(char *str);
[global _strlen]
_strlen:
push ebp
mov ebp, esp
push esi
push ebx
mov esi, [ebp+8]
xor ebx, ebx
strlenloop:
lodsb
or al, al
jz strlendone
inc ebx
jmp strlenloop
strlendone:
mov eax, ebx
pop ebx
pop esi
pop ebp
ret
;this function invalidates the page directory/table entry that
; would be used to access the memory address given in the parameter
;extern void invlpg_(dword addr);
[global _invlpg_]
_invlpg_:
mov eax, [esp+4]
invlpg [eax]
ret
;
;void writeCursorPosition(word pos)
;
[global _writeCursorPosition]
_writeCursorPosition:
push ebp
mov ebp, esp
push eax
push ebx
push edx
mov eax, [ebp+8] ;cursor position in ax
mov bl, al
mov dx, 0x03D4
mov al, 0x0E
out dx, al
inc dx
mov al, ah
out dx, al
dec dx
mov al, 0x0F
out dx, al
inc dx
mov al, bl
out dx, al
pop edx
pop ebx
pop eax
pop ebp
ret
;
;word getCursorPosition()
;
[global _getCursorPosition]
_getCursorPosition:
push ebx
push edx
xor eax, eax
mov dx, 0x03D4
mov al, 0x0E
out dx, al
inc dx
in al, dx
mov bl, al
dec dx
mov al, 0x0F
out dx, al
inc dx
in al, dx
mov ah, bl
pop edx
pop ebx
ret
;
;int puts(char *str)
;
[global _puts]
_puts:
push ebp
mov ebp, esp
push esi
push eax
mov esi, [ebp+8] ;esi = to string
puts_loop:
lodsb
cmp al, 0
jz puts_done
push eax
call _putc
add esp, 4
jmp puts_loop
puts_done:
pop eax
pop esi
pop ebp
ret
[global _putDecu]
_putDecu:
push ebp
mov ebp, esp
sub esp, 24
mov DWORD [ebp-4], 1
mov BYTE [ebp-5], 0
L2:
mov edx, DWORD [ebp+8]
mov eax, -858993459
mul edx
mov eax, edx
shr eax, 3
cmp eax, DWORD [ebp-4]
jae L4
jmp L3
L4:
mov eax, DWORD [ebp-4]
mov edx, eax
sal edx, 2
add edx, eax
lea eax, [edx+edx]
mov DWORD [ebp-4], eax
jmp L2
L3:
nop
L5:
cmp DWORD [ebp-4], 1
ja L7
jmp L6
L7:
mov edx, DWORD [ebp+8]
mov eax, edx
mov edx, 0
div DWORD [ebp-4]
mov DWORD [ebp-12], eax
mov al, BYTE [ebp-12]
mov BYTE [ebp-5], al
mov eax, 0
mov al, BYTE [ebp-5]
imul eax, DWORD [ebp-4]
sub DWORD [ebp+8], eax
mov edx, DWORD [ebp-4]
mov eax, -858993459
mul edx
mov eax, edx
shr eax, 3
mov DWORD [ebp-4], eax
lea eax, [ebp-5]
add BYTE [eax], 48
sub esp, 12
mov eax, 0
mov al, BYTE [ebp-5]
push eax
call _putc
add esp, 16
jmp L5
L6:
sub esp, 12
mov al, BYTE [ebp+8]
add eax, 48
and eax, 255
push eax
call _putc
add esp, 16
leave
ret
[global _putDec]
_putDec:
push ebp
mov ebp, esp
sub esp, 24
cmp DWORD [ebp+8], 0
jns L9
sub esp, 12
push 45
call _putc
add esp, 16
neg DWORD [ebp+8]
L9:
mov DWORD [ebp-4], 1
mov BYTE [ebp-5], 0
L10:
mov eax, DWORD [ebp+8]
cmp eax, DWORD [ebp-4]
jae L12
jmp L11
L12:
mov eax, DWORD [ebp-4]
mov edx, eax
sal edx, 2
add edx, eax
lea eax, [edx+edx]
mov DWORD [ebp-4], eax
jmp L10
L11:
mov edx, DWORD [ebp-4]
mov eax, -858993459
mul edx
mov eax, edx
shr eax, 3
mov DWORD [ebp-4], eax
L13:
cmp DWORD [ebp-4], 1
ja L15
jmp L14
L15:
mov edx, DWORD [ebp+8]
mov eax, edx
mov edx, 0
div DWORD [ebp-4]
mov DWORD [ebp-12], eax
mov al, BYTE [ebp-12]
mov BYTE [ebp-5], al
mov eax, 0
mov al, BYTE [ebp-5]
imul eax, DWORD [ebp-4]
sub DWORD [ebp+8], eax
mov edx, DWORD [ebp-4]
mov eax, -858993459
mul edx
mov eax, edx
shr eax, 3
mov DWORD [ebp-4], eax
lea eax, [ebp-5]
add BYTE [eax], 48
sub esp, 12
mov eax, 0
mov al, BYTE [ebp-5]
push eax
call _putc
add esp, 16
jmp L13
L14:
sub esp, 12
mov al, BYTE [ebp+8]
add eax, 48
and eax, 255
push eax
call _putc
add esp, 16
leave
ret

117
us/idt.inc Normal file
View File

@ -0,0 +1,117 @@
;idt.inc
;Author: Josh Holtrop
;Date: 10/30/03
;Modified: 03/02/04
idtr:
dw 50*8-1 ;size of idt
dd IDT_V ;address of idt
%macro isr_label 1
isr_%1:
push eax
mov eax, %1
jmp isr_main
%endmacro
isr_label 0
isr_label 1
isr_label 2
isr_label 3
isr_label 4
isr_label 5
isr_label 6
isr_label 7
isr_label 8
isr_label 9
isr_label 10
isr_label 11
isr_label 12
isr_label 13
isr_label 14
isr_label 15
isr_label 16
isr_label 17
isr_label 18
isr_label 19
isr_label 20
isr_label 21
isr_label 22
isr_label 23
isr_label 24
isr_label 25
isr_label 26
isr_label 27
isr_label 28
isr_label 29
isr_label 30
isr_label 31
isr_label 32
isr_label 33
isr_label 34
isr_label 35
isr_label 36
isr_label 37
isr_label 38
isr_label 39
isr_label 40
isr_label 41
isr_label 42
isr_label 43
isr_label 44
isr_label 45
isr_label 46
isr_label 47
isr_label 48
isr_label 49
isr_main:
cmp eax, 0x30
jz isr_syscall
pusha
push ds
push es
push eax
call _isr
add esp, 4
pop es
pop ds
popa
pop eax
iret
isr_syscall:
pop eax ;syscall function number
pusha
push ds
push es
sc1:
cmp eax, 1 ;syscall 1 - putc
jnz sc2
push ebx
call _putc
add esp, 4
jmp scdone
sc2:
scdone:
pop es
pop ds
popa
iret

102
us/kernel.asm Normal file
View File

@ -0,0 +1,102 @@
;kernel.asm
;Author: Josh Holtrop
;Date: 10/30/03
;Modified: 10/30/03
%define GDT_P 0x100000; ;1mb physical - Global Descriptor Table space
%define GDT_V GDT_P+0xC0000000
%define IDT_P 0x102000 ;1mb+8kb - Interrupt Descriptor Table space
%define IDT_V IDT_P+0xC0000000
%define PDBR_P 0x104000 ;1mb+16kb - Page Directory Base Register (first PD)
%define PDBR_V PDBR_P+0xC0000000
%define LOPT_P 0x105000 ;1mb+20kb - LOw Page Table for mapping first 4mb
%define LOPT_V LOPT_P+0xC0000000
%define KERNEL_P 0x106000 ;1mb+24kb - the kernel's physical address
%define KERNEL_V KERNEL_P+0xC0000000 ;3gb+1mb+24kb, the virtual address of the kernel
[global _start]
[extern _isr]
[extern _k_init]
[extern _putc]
bits 32
;This is where the kernel begins execution
;At this point, the temporary gdt is set up to "map" 0xC000_0000 to 0x0.
;We must enable paging with the first 4mb mapped 1:1 virtual:physical
; and with the 4mb starting at 0xC000_0000 mapped to the first 4mb physical.
;Then we can start using our "real" gdt, then unmap the lower 4mb.
_start:
cli ;if they weren't already off
xor eax, eax
mov edi, PDBR_V
mov ecx, 1024 ;clear the PDBR
rep stosd
mov [PDBR_V], dword LOPT_P|0x03 ;store the physical address of the LOw Page Table (read/write, present)
mov [PDBR_V+0xC00], dword LOPT_P|0x03 ;store the physical address of the LOw Page Table (read/write, present)
mov edi, LOPT_V
mov ecx, 1024
mov eax, 0x03 ;starting physical address = 0x0 (read/write, present flags)
fill_lopt_loop: ;fill the page table
stosd
add eax, 4096 ;increment next phsyical address by 4kb
loop fill_lopt_loop
mov eax, PDBR_P
mov cr3, eax ;store the Page Directory Base Address
mov eax, cr0
or eax, 0x80000000 ;set page enable bit
mov cr0, eax ;now paging is active!
mov edi, GDT_V
mov esi, gdt
mov ecx, gdt_end-gdt
copy_gdt:
lodsb
stosb
loop copy_gdt
mov edi, IDT_V ;destination
mov esi, isr_0 ;address of isr0
mov edx, isr_1-isr_0 ;distance between isr labels
mov ecx, 50 ;number of isrlabels
fill_idt:
mov ebx, esi
mov ax, si
stosw ;0 offset 15:0
mov ax, KERNEL_CODE
stosw ;2 selector 15:0
mov ax, 0x8E00
stosw ;4 [P][DPL][0][TYPE][0][0][0][0][0][0][0][0]
shr esi, 16
mov ax, si
stosw ;6 offset 31:16
mov esi, ebx
add esi, edx
loop fill_idt
mov word [IDT_V+0x30*8+4], 0xEE00 ;interrupt 0x30 has user priviledges
lgdt [gdtr] ;load gdt
jmp KERNEL_CODE:newgdtcontinue
newgdtcontinue:
mov ax, KERNEL_DATA
mov es, ax
mov ds, ax
mov gs, ax
mov fs, ax
mov ss, ax
mov esp, 0xc0200000 ;stack just under 3gb+2mb, moves downward
lidt [idtr] ;load idt
call _k_init
haltit:
hlt ;halt processor when k_init is done
jmp haltit ;shouldn't get here...
%include "gdt.inc"
%include "idt.inc"