Skip to content

Commit

Permalink
Scanning for RSD PTR. Updated bootloader code so that loads kernel at…
Browse files Browse the repository at this point in the history
… a different address and don't clobber ACPI tables.
  • Loading branch information
shoily committed Nov 14, 2020
1 parent f834fb5 commit aea7589
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 14 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<b>XIS</b> operating system needs only 2MB of memory to run.<br>

It supports boot loader, kernel with protected mode, paging, E820 enumeration, interrupt and exception handling, system calls, user mode, local APIC and multiprocessor.
It supports boot loader, kernel with protected mode, paging, E820 enumeration, interrupt and exception handling, system calls, user mode, local APIC, ACPI and multiprocessor.

<b>Instructions for building kernel -</b><br>
cat krnlconst.hdr | awk 'BEGIN{print "#ifndef _KERNEL_CONST_H"};{print "#define " $1 " " $2};END{print "#endif";}' > krnlconst.h<br>
Expand All @@ -23,8 +23,9 @@ gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nosta
gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding -fno-pic -Wall -Wextra -Werror -c usermode.c -o usermode.o<br>
gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding -fno-pic -Wall -Wextra -Werror -c lock.c -o lock.o<br>
gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding -fno-pic -Wall -Wextra -Werror -c debug.c -o debug.o<br>
gcc -m32 -std=gnu99 -nostdlib -nostdinc -fno-builtin -fno-stack-protector -nostartfiles -nodefaultlibs -ffreestanding -fno-pic -Wall -Wextra -Werror -c acpi.c -o acpi.o<br>

ld -static -T kernel32.ld -m elf_i386 -nostdlib --nmagic boot32.o util.o system.o apic.o smp.o setup32.o handlr32.o memory.o page32.o usermode.o lock.o debug.o start.o -o xiskernel.elf<br>
ld -static -T kernel32.ld -m elf_i386 -nostdlib --nmagic boot32.o util.o system.o apic.o smp.o setup32.o handlr32.o memory.o page32.o usermode.o lock.o acpi.o debug.o start.o -o xiskernel.elf<br>
objcopy -O binary xiskernel.elf xiskernel.bin<br>

<b>Instructions for building boot loader -</b><br>
Expand Down
62 changes: 62 additions & 0 deletions acpi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*****************************************************************************/
/* File: acpi.c */
/* */
/* Description: Source file for ACPI. */
/* */
/* Author: Shoily O Rahman <[email protected]> */
/* */
/* Date: Nov 3, 2020 */
/* */
/*****************************************************************************/

#include "acpi.h"
#include "debug.h"
#include "krnlconst.h"
#include "debug.h"

extern int ebda;

void *rsd_ptr;

#define CHECK_RSD_PTR(p) (((char*)p)[0]=='R' && \
((char*)p)[1]=='S' && \
((char*)p)[2]=='D' && \
((char*)p)[3]==' ' && \
((char*)p)[4]=='P' && \
((char*)p)[5]=='T' && \
((char*)p)[6]=='R' && \
((char*)p)[7]==' ')

void acpi_find_rsdp() {

int phase = 0;
int cur, end;

rsd_ptr = 0;

cur = ebda + KERNEL_VIRT_ADDR;
end = ebda + 1024;

while(phase < 2) {

if (cur == end) {

phase++;

if (phase == 1) {

cur = 0xE0000 + KERNEL_VIRT_ADDR;
end = cur + 0x20000;
}
}

if (CHECK_RSD_PTR(cur)) {

rsd_ptr = (void*)cur;
printf(KERNEL_INFO, "RSD PTR: %p\n", rsd_ptr-KERNEL_VIRT_ADDR);
break;
}

cur += 16;
}
}
17 changes: 17 additions & 0 deletions acpi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*****************************************************************************/
/* File: acpi.h */
/* */
/* Description: Header file for ACPI. */
/* */
/* Author: Shoily O Rahman <[email protected]> */
/* */
/* Date: Nov 3, 2020 */
/* */
/*****************************************************************************/

#ifndef _ACPI_H
#define _ACPI_H

void acpi_find_rsdp();

#endif
23 changes: 15 additions & 8 deletions bootldr.S
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
.text
.globl _start
_start:
movb %dl, boot_drive

movb %dl, boot_drive


// clear screen
movb $0, %ah
Expand Down Expand Up @@ -114,14 +116,16 @@ a20_line:
movw %ax, %ss

// memory buffer location of disk address packet
movw dap, %si
movw $dap, %si
xorw %ax, %ax
movw %ax, 4(%si)
movw %ax, 4(%si)



movw $KERNEL_SECTORS, %ax

// check kernel can fit into existing memory
cmpw $160, %ax
cmpw $255, %ax
ja kernel_too_big

// read enough sectors to load kernel
Expand All @@ -137,7 +141,7 @@ a20_line:
movl %ebx, 12(%si)

// segment of buffer address
movw $0x5000, 6(%si)
movw $0x7e0, 6(%si)

// set stack location at 4K offset
movl $0x1000, %ebp
Expand All @@ -147,7 +151,10 @@ a20_line:
movb $0x42, %ah
int $0x13

jc lba_read_error
jc lba_read_error




// initialize protected mode
cli
Expand All @@ -170,7 +177,7 @@ a20_line:
orl $1, %eax
movl %eax, %cr0

cli
cli

// jump to protected mode
ljmpl *codesegaddr
Expand Down Expand Up @@ -210,7 +217,7 @@ protectedmodeaddress:
// copy kernel to 1 MB offset
cld
movl $(KERNEL_SECTORS * 0x200 / 4), %ecx
movl $0x50000, %esi
movl $0x7e00, %esi
movl $0x100000, %edi
rep movsl

Expand Down
6 changes: 3 additions & 3 deletions krnlconst.hdr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
VIDEO_BUFFER 0xb8000
E820_MAP_ADDRESS 0x1008
E820_MAP_COUNT 0x1004
PROTECTED_MODE_STACK 0xf000
E820_MAP_ADDRESS 0x2008
E820_MAP_COUNT 0x2004
PROTECTED_MODE_STACK 0x1000
AP_FIRST_STACK 0xfff0
AP_LAPIC_BASE_REGISTER 0xfff4
AP_COUNT_PHYS_ADDR 0xfff8
Expand Down
2 changes: 1 addition & 1 deletion start.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int start_kernel(void) {
setup32();
init_memory();
bda_read_table();
//acpi_find_rsdp();
acpi_find_rsdp();
usermode_load_first_program();
smp_start();
initialize_usermode();
Expand Down

0 comments on commit aea7589

Please sign in to comment.