Skip to content

Commit

Permalink
Moved initial usermode program from usermode.c to a separate binary g…
Browse files Browse the repository at this point in the history
…enerate from um.S and um.ld. Later, embed the binary into boot32.
  • Loading branch information
shoily committed Jul 7, 2021
1 parent 91035c8 commit a2ff1fc
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 33 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ It supports boot loader, kernel with protected mode, paging, E820 enumeration, i
cat krnlconst.hdr | awk 'BEGIN{print "#ifndef _KERNEL_CONST_H"};{print "#define " $1 " " $2};END{print "#endif";}' > krnlconst.h<br>
cat krnlconst.hdr | awk '{print ".equ " $1 ", " $2};' > krnlconst.S<br>

as --32 um.S -o um.o<br>
ld --static -T um.ld -m elf_i386 -nostdlib --nmagic -o um.elf um.o<br>
objcopy -O binary um.elf um.bin<br>

as --32 mpinit.S -o mpinit.o<br>
ld -static -T mpinit.ld -m elf_i386 -nostdlib --nmagic -o mpinit.elf mpinit.o<br>
objcopy -O binary mpinit.elf mpinit.bin<br>
Expand Down
15 changes: 12 additions & 3 deletions boot32.S
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,24 @@ highaddress:
loop:
jmp loop

// load smp binary code here
.section mp_init, "ax"
.global init_ap
.align 8
init_ap:
.incbin "mpinit.bin"

.global init_ap_size
.align 8
.global init_ap_size

init_ap_size:
.int init_ap_size - init_ap

// load um binary code here
.section um_init, "ax"
.global init_um
.align 4096
init_um:
.incbin "um.bin"

.global init_um_size
init_um_size:
.int init_um_size - init_um
3 changes: 2 additions & 1 deletion kernel32.ld
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ SECTIONS
.text : {*(.text); }
.data : {*(.data); }
.bss : {*(.bss); }
um_size = SIZEOF(um);
. = ALIGN(8);
.um_init : {*(.um_init); }
. = ALIGN(8);
.mp_init : {*(.mp_init); }
. = ALIGN(4096);
Expand Down
1 change: 0 additions & 1 deletion start.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ int start_kernel(void) {
bda_read_table();
acpi_init();
ioapic_init();
usermode_load_first_program();
smp_start();
initialize_usermode();
switch_to_um();
Expand Down
32 changes: 32 additions & 0 deletions um.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*****************************************************************************/
/* File: um.S */
/* */
/* Description: Usermode code. */
/* */
/* Author: Shoily O Rahman <[email protected]> */
/* */
/* Date: Jun 20, 2021 */
/* */
/*****************************************************************************/

.code32
.text

.globl _um_start
_um_start:

push %ebp
mov %esp,%ebp
sub $0x4,%esp
movl $0x1,-0x4(%ebp)
loop:
int $0x80
mov -0x4(%ebp),%eax
inc %eax
mov %eax,-0x4(%ebp)
jmp loop
nop
leave
ret


34 changes: 34 additions & 0 deletions um.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*****************************************************************************/
/* File: mpinit.ld */
/* */
/* Description: ld script file for initializing multiple (application) */
/* processors. */
/* */
/* Author: Shoily O Rahman <[email protected]> */
/* */
/* Date: Oct 7, 2020 */
/* */
/*****************************************************************************/

OUTPUT_FORMAT(elf32-i386)
OUTPUT_ARCH(i386)

/* This will overwrite default boot sector */
ENTRY(_um_start)

PHDRS {
text PT_LOAD FILEHDR PHDRS;
data PT_LOAD;
bss PT_LOAD;
}

SECTIONS
{
. = 0x10000000;
.text :
{*(.text); } : text
.data :
{*(.data); } : data
.bss :
{*(.bss); } : bss
}
33 changes: 6 additions & 27 deletions usermode.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,17 @@
#include "page32.h"
#include "setup32.h"
#include "system.h"
#include "debug.h"

char __attribute__((aligned(4096))) um_pg_table[MAX_NUM_SMPS][PAGE_SIZE];
char __attribute__((aligned(4096))) user_mode_program[PAGE_SIZE];

#define USER_MODE_STACK_SIZE 8192
char __attribute__((aligned(8))) user_mode_stack[MAX_NUM_SMPS][USER_MODE_STACK_SIZE];

extern int um, um_size;
extern int _kernel_pg_dir;

__attribute__((section("um"))) __attribute__((aligned(4096))) void test_usermode_func() {
int *a = (int*)((char*) USER_MODE_VIRT_TEXT + 0x100);
int i = 1;

__asm__ __volatile__("int $128;" : : :);
*a = i++;

__asm__ __volatile("movl %0, %%ecx; jmp *%%ecx;" : : "i" (USER_MODE_VIRT_TEXT+0x16): "%ecx");
}

void usermode_load_first_program() {

char *s = (char *)test_usermode_func;
char *d = user_mode_program;
int i;

memset(user_mode_program, sizeof(user_mode_program), 0);

for(i=(int)&um;i<(int)&um+(int)&um_size;i++) {
*d++ = *s++;
}
}
extern int init_um;
extern int init_um_size;

void switch_to_um() {

Expand Down Expand Up @@ -79,12 +58,12 @@ void initialize_usermode() {
memset(&user_mode_stack[CUR_CPU][0], USER_MODE_STACK_SIZE, 0);

pgtable[0] = (pte_t*)&um_pg_table[CUR_CPU][0];

build_pagetable((pgd_t*)((int)&_kernel_pg_dir + (PAGE_SIZE*CUR_CPU)),
pgtable,
(int)user_mode_program-KERNEL_VIRT_ADDR,
(int)&init_um-KERNEL_VIRT_ADDR,
USER_MODE_VIRT_TEXT,
(int)&um_size,
(int)init_um_size,
PGD_PRESENT | PGD_WRITE | PGD_USER, PTE_PRESENT | PTE_WRITE | PTE_USER);

build_pagetable((pgd_t*)((int)&_kernel_pg_dir + (PAGE_SIZE*CUR_CPU)),
Expand Down
1 change: 0 additions & 1 deletion usermode.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
#define USER_MODE_VIRT_TEXT 0x10000000
#define USER_MODE_VIRT_STACK 0x10003000

void usermode_load_first_program();
void initialize_usermode();
void switch_to_um();

Expand Down

0 comments on commit a2ff1fc

Please sign in to comment.