Skip to content

Commit 1999fa7

Browse files
committed
Fixed Usermode Segment Loading
- Change : Introduced uint16_t data_sel and uint16_t code_sel variables. - Fix : Modified the asm volatile block to load ds , es , fs , and gs directly from the 16-bit register operand, which satisfies the assembler's requirement for segment register moves. - Improved Safety : Updated the pushl sequence for ss and cs to explicitly cast the 16-bit selectors to 32-bit to maintain stack alignment for iret .
1 parent 1391c28 commit 1999fa7

3 files changed

Lines changed: 18 additions & 13 deletions

File tree

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ C_SRCS := $(wildcard $(SRC_DIR)/*.c $(SRC_DIR)/*/*.c $(SRC_DIR)/*/*/*.c)
1818
ASM_OBJS := $(patsubst $(SRC_DIR)/%.s,$(BUILD_DIR)/%.o,$(ASM_SRCS))
1919
C_OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/%.o,$(C_SRCS))
2020

21-
OBJS := $(ASM_OBJS) $(C_OBJS)
21+
BOOT_OBJ := $(BUILD_DIR)/boot.o
22+
OTHER_OBJS := $(filter-out $(BOOT_OBJ),$(ASM_OBJS) $(C_OBJS))
23+
OBJS := $(BOOT_OBJ) $(OTHER_OBJS)
2224

2325
ifneq ($(MODEL_BLOB),)
2426
OBJS += $(MODEL_OBJ)

src/ramdisk.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ static uint32_t ramdisk_read(fs_node_t* node, uint32_t offset, uint32_t size, ui
3030
}
3131

3232
fs_node_t* ramdisk_init(uint32_t module_start, uint32_t module_size) {
33+
(void)module_size;
3334
initrd_base = (uint8_t*)module_start;
3435
initrd_header = (initrd_header_t*)initrd_base;
3536
file_headers = (initrd_file_header_t*)(initrd_base + sizeof(initrd_header_t));

src/usermode.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,26 @@
44
#define USER_DATA_SEL 0x23
55

66
void switch_to_user_mode(uint32_t entry_point, uint32_t user_stack) {
7+
uint16_t data_sel = USER_DATA_SEL;
8+
uint16_t code_sel = USER_CODE_SEL;
9+
710
asm volatile(
811
"cli\n"
9-
"mov %0, %%ax\n"
10-
"mov %%ax, %%ds\n"
11-
"mov %%ax, %%es\n"
12-
"mov %%ax, %%fs\n"
13-
"mov %%ax, %%gs\n"
14-
"pushl %0\n"
15-
"pushl %1\n"
12+
"mov %0, %%ds\n"
13+
"mov %0, %%es\n"
14+
"mov %0, %%fs\n"
15+
"mov %0, %%gs\n"
16+
"pushl %1\n" // ss
17+
"pushl %2\n" // esp
1618
"pushfl\n"
1719
"popl %%eax\n"
1820
"or $0x200, %%eax\n"
19-
"pushl %%eax\n"
20-
"pushl %2\n"
21-
"pushl %3\n"
21+
"pushl %%eax\n" // eflags
22+
"pushl %3\n" // cs
23+
"pushl %4\n" // eip
2224
"iret\n"
2325
:
24-
: "r"(USER_DATA_SEL), "r"(user_stack), "r"(USER_CODE_SEL), "r"(entry_point)
25-
: "eax"
26+
: "r"(data_sel), "r"((uint32_t)data_sel), "r"(user_stack), "r"((uint32_t)code_sel), "r"(entry_point)
27+
: "eax", "memory"
2628
);
2729
}

0 commit comments

Comments
 (0)