Skip to content

Commit accdbce

Browse files
committed
Softcode GDT table selectors
1 parent de6e163 commit accdbce

File tree

19 files changed

+122
-55
lines changed

19 files changed

+122
-55
lines changed

include/fuzzy/kernel/process/process.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
#include<stddef.h>
44

55
#define MAX_PROCESS 10
6-
#define GDT_TABLE_SIZE MAX_PROCESS*2+3
6+
// each process will have CS and DS, and kernel
7+
// CS and DS is already included in GDT standard table.
8+
#define GDT_TABLE_SIZE ((MAX_PROCESS-1)*2+GDT_STD_SIZE)
79

810
typedef char ARGV[PROCESS_MAX_ARGC][PROCESS_MAX_ARG_LEN];
911

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
; constants only
2+
3+
; START_ENSURE_SAME_gdt_h
4+
5+
GDT_STD_SELECTOR_NULL EQU 0
6+
GDT_STD_SELECTOR_KERNEL_CS EQU 1
7+
GDT_STD_SELECTOR_KERNEL_DS EQU 2
8+
GDT_STD_SELECTOR_ABS16_CS EQU 3
9+
GDT_STD_SELECTOR_ABS16_DS EQU 4
10+
GDT_STD_SELECTOR_ABS32_DS EQU 6
11+
GDT_STD_SIZE EQU 5 ; FIX
12+
13+
GDT_STD_SIZEOF_ENTRY EQU 8
14+
; END_ENSURE_SAME_gdt_h
15+
16+
GDT_NULL_CS EQU (GDT_STD_SELECTOR_NULL*GDT_STD_SIZEOF_ENTRY)
17+
GDT_KERNEL_CS EQU (GDT_STD_SELECTOR_KERNEL_CS*GDT_STD_SIZEOF_ENTRY)
18+
GDT_KERNEL_DS EQU (GDT_STD_SELECTOR_KERNEL_DS*GDT_STD_SIZEOF_ENTRY)
19+
GDT_ABS16_CS EQU (GDT_STD_SELECTOR_ABS16_CS*GDT_STD_SIZEOF_ENTRY)
20+
GDT_ABS16_DS EQU (GDT_STD_SELECTOR_ABS16_DS*GDT_STD_SIZEOF_ENTRY)
21+
GDT_ABS32_DS EQU (GDT_STD_SELECTOR_ABS32_DS*GDT_STD_SIZEOF_ENTRY)

include/fuzzy/memmgr/tables/gdt.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,18 @@
33
// Min GDT_TABLE_SIZE: 5
44
// Max GDT_TABLE_SIZE: 8192
55

6+
// START_ENSURE_SAME_gdt_asm
7+
#define GDT_STD_SELECTOR_NULL 0
8+
#define GDT_STD_SELECTOR_KERNEL_CS 1
9+
#define GDT_STD_SELECTOR_KERNEL_DS 2
10+
#define GDT_STD_SELECTOR_ABS16_CS 3
11+
#define GDT_STD_SELECTOR_ABS16_DS 4
12+
#define GDT_STD_SELECTOR_ABS32_DS 6
13+
#define GDT_STD_SIZE 5 // FIX
14+
// Entries on and after GDT_STD_SIZE are reserved for applications
15+
// and are in CS, DS order for each app.
16+
// END_ENSURE_SAME_gdt_asm
17+
618
#pragma pack(push, 1)
719
struct GDTReference {
820
unsigned short size;
@@ -18,6 +30,8 @@ struct GDTEntry {
1830
};
1931
#pragma pack(pop)
2032

33+
#define GDT_KERNEL_CS (GDT_STD_SELECTOR_KERNEL_CS*sizeof(struct GDTEntry))
34+
2135
int get_gdt_baseaddress(struct GDTEntry gdt_table[], unsigned int table_size, int entry_id);
2236

2337
void populate_gdt_entry(struct GDTEntry *entry,

src/bootloader/Makefile.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ $(bt_stage1): $(SRC_BOOTLOADER)/stage1.asm $(SRC_BOOTLOADER)/constants.asm $(SRC
2020

2121
$(bt_stage2).elf: $(SRC_BOOTLOADER)/stage2.asm $(SRC_BOOTLOADER)/stage2.c $(INCLUDE_DIR)/memmgr/tables/gdt.h $(SRC_MEMMGR)/tables/gdt.c $(SRC_BOOTLOADER)/io.asm $(SRC_BOOTLOADER)/constants.asm $(BUILD_LIB_UTILS)/libutils_16 $(BUILD_DRIVERS)/display/libtm_bios $(BUILD_DRIVERS)/disk/libdisk_16
2222
mkdir -p $$(dirname $(bt_stage2))
23-
nasm -o $(BUILD_BOOTLOADER)/stage2_asm.o -f elf32 -i $(SRC_BOOTLOADER)/ $(SRC_BOOTLOADER)/stage2.asm
23+
$(NASM) -o $(BUILD_BOOTLOADER)/stage2_asm.o -i $(SRC_BOOTLOADER)/ $(SRC_BOOTLOADER)/stage2.asm
2424
$(CC) -m16 -fno-pie -c -Isrc \
2525
-Iinclude \
2626
-D SECTOR_START_SHARED_LIBRARY=$(SECTOR_START_SHARED_LIBRARY) \

src/bootloader/stage2.asm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
; Fuzzy Bootloader Stage 2
22
%include "constants.asm"
33
%include "io.asm"
4+
%include "fuzzy/memmgr/tables/gdt.asm"
45

56
[BITS 16]
67

@@ -38,7 +39,7 @@ global call_int_0x15
3839

3940
; __TEST_INJECT_BT2__: mov eax, 0x198A65C3
4041
; __TEST_INJECT_BT2__: HLT
41-
jmp 0x08:0x0000 ; address of smart kernel init
42+
jmp GDT_KERNEL_CS:0x0000 ; address of smart kernel init
4243

4344
label_exit:
4445
HLT

src/bootloader/stage2.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <fuzzy/memmgr/tables/gdt.h>
12
#include <fuzzy/memmgr/layout.h>
23

34
#include <lib/utils/color.h>
@@ -8,8 +9,7 @@
89

910
#include "memmgr/tables/gdt.c"
1011

11-
#define GDT_TABLE_SIZE 5
12-
struct GDTEntry gdt_table[GDT_TABLE_SIZE];
12+
struct GDTEntry gdt_table[GDT_STD_SIZE];
1313
struct GDTReference gdtr;
1414

1515
char DIGIT_TO_HEX[] = "0123456789ABCDEF";
@@ -87,7 +87,7 @@ void entry_stage() {
8787
load_static_library();
8888
load_kernel();
8989

90-
populate_gdt_table(&gdtr, gdt_table, GDT_TABLE_SIZE, 0);
90+
populate_gdt_table(&gdtr, gdt_table, GDT_STD_SIZE, 0);
9191

9292
// Enter_protected_mode never returns.
9393
print_log("Loading GDT Table and entering protected mode");

src/drivers/disk/disk.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// written for protected mode
2+
#include <fuzzy/kernel/process/process.h>
3+
24
#include <lib/utils/logging.h>
35
#include <fuzzy/real_mode/client.h>
46

@@ -29,5 +31,10 @@ int load_sectors(unsigned int full_address,
2931
unsigned int status = eax >> 8;
3032
print_info("[load_sectors] [dev %x]:%x -> mem:%x, cnt: %d; err: %x",
3133
drive, lba, full_address, count, status);
34+
if(status == 0) {
35+
// no error copy from temporary address to dst address.
36+
// TODO: redefine GDT table order.
37+
// kernel_memncpy_absolute(int dst_ds, char *dst_address, int src_ds, char *src_address, size_t size);
38+
}
3239
return status;
3340
}

src/drivers/display/Makefile.mk

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ $(BUILD_DRIVERS)/display/libtm_bios: $(SRC_DRIVERS)/display/text_mode_bios.c $(S
22
# 16 bit mode
33
mkdir -p $(BUILD_DRIVERS)/display/
44
$(CC) -m16 -fno-pie -c -Isrc -o $(BUILD_DRIVERS)/display/text_mode_bios_c.o $(SRC_DRIVERS)/display/text_mode_bios.c
5-
nasm -o $(SRC_DRIVERS)/display/text_mode_bios_asm.o -f elf32 $(SRC_DRIVERS)/display/text_mode_bios.asm
6-
ar rc $@ $(BUILD_DRIVERS)/display/text_mode_bios_c.o $(SRC_DRIVERS)/display/text_mode_bios_asm.o
5+
$(NASM) -o $(BUILD_DRIVERS)/display/text_mode_bios_asm.o $(SRC_DRIVERS)/display/text_mode_bios.asm
6+
ar rc $@ $(BUILD_DRIVERS)/display/text_mode_bios_c.o $(BUILD_DRIVERS)/display/text_mode_bios_asm.o
77

88
$(BUILD_DRIVERS)/display/libtm_vga: $(SRC_DRIVERS)/display/text_mode_vga.c $(SRC_DRIVERS)/display/text_mode_vga.asm $(SRC_DRIVERS)/display/text_mode.h
99
mkdir -p $(BUILD_DRIVERS)/display/
1010
$(CC) -m32 -fno-pie -c -Isrc -o $(BUILD_DRIVERS)/display/text_mode_vga_c.o $(SRC_DRIVERS)/display/text_mode_vga.c
11-
nasm -o $(SRC_DRIVERS)/display/text_mode_vga_asm.o -f elf32 $(SRC_DRIVERS)/display/text_mode_vga.asm
12-
ar rc $@ $(BUILD_DRIVERS)/display/text_mode_vga_c.o $(SRC_DRIVERS)/display/text_mode_vga_asm.o
11+
$(NASM) -o $(BUILD_DRIVERS)/display/text_mode_vga_asm.o $(SRC_DRIVERS)/display/text_mode_vga.asm
12+
ar rc $@ $(BUILD_DRIVERS)/display/text_mode_vga_c.o $(BUILD_DRIVERS)/display/text_mode_vga_asm.o

src/drivers/display/text_mode_vga.asm

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
%include "fuzzy/memmgr/tables/gdt.asm"
2+
13
[BITS 32]
24

35
global _low_put_char
@@ -14,12 +16,12 @@ global _low_flush
1416
; push edi
1517

1618
push ds
17-
mov eax, 0x20
19+
mov eax, GDT_ABS16_DS
1820
mov ds, eax ; Absolute memory address
1921

2022
mov ebx,[ebp + 0x10] ; (ROW_WIDTH*y+x)
2123
shl ebx, 1
22-
add ebx,0xb8000
24+
add ebx, 0xb8000
2325
mov al, [ebp + 0x8] ; (char)
2426
mov ah, [ebp + 0xc] ; (color)
2527
mov [ebx], ax
@@ -39,7 +41,7 @@ global _low_flush
3941
; push esi
4042
; push edi
4143
push ds
42-
mov eax, 0x20
44+
mov eax, GDT_ABS16_DS
4345
mov ds, eax ; Absolute memory address
4446

4547
; Copy char+colors in Row Order Format
@@ -71,7 +73,7 @@ global _low_flush
7173
push esi
7274
push edi
7375
push es
74-
mov eax, 0x20
76+
mov eax, GDT_ABS16_DS
7577
mov es, eax ; Absolute memory address
7678

7779
mov esi, [ebp + 0x8] ; (buffer)

src/kernel/core.asm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
%include "fuzzy/memmgr/layout.asm"
2+
%include "fuzzy/memmgr/tables/gdt.asm"
23

34
[BITS 32]
45

@@ -9,7 +10,7 @@ global kernel_core_entry_asm
910
[SECTION .text]
1011
; protected mode real entry point.
1112
CLI
12-
mov ax, 0x10
13+
mov ax, GDT_KERNEL_DS
1314
mov es, ax
1415
mov ss, ax
1516
mov ds, ax

0 commit comments

Comments
 (0)