Skip to content

Commit fa8d51d

Browse files
committed
Fix GDT table limits
1 parent f6cafaf commit fa8d51d

File tree

4 files changed

+46
-27
lines changed

4 files changed

+46
-27
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ binaries: $(bt_stage1) $(bt_stage2) $(kernel_core) $(rm_static)
107107
SECTOR_COUNT_BT_STAGE1 = 1
108108
SECTOR_COUNT_SHARED_LIBRARY = 1
109109
SECTOR_COUNT_BT_STAGE2 = 12
110-
SECTOR_COUNT_KERNEL = 70
110+
SECTOR_COUNT_KERNEL = 75
111111

112112
SECTOR_START_BT_STAGE1 = 0
113113
SECTOR_START_SHARED_LIBRARY = $(shell expr $(SECTOR_START_BT_STAGE1) + $(SECTOR_COUNT_BT_STAGE1) )

include/fuzzy/memmgr/tables/gdt.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ struct GDTEntry {
3030
};
3131
#pragma pack(pop)
3232

33+
#define GDT_ENTRY_FLAG_32_BIT_SELECTOR 1
34+
3335
#define GDT_NULL_CS (GDT_STD_SELECTOR_NULL*sizeof(struct GDTEntry))
3436
#define GDT_KERNEL_CS (GDT_STD_SELECTOR_KERNEL_CS*sizeof(struct GDTEntry))
3537
#define GDT_KERNEL_DS (GDT_STD_SELECTOR_KERNEL_DS*sizeof(struct GDTEntry))
@@ -41,7 +43,7 @@ int get_gdt_baseaddress(struct GDTEntry gdt_table[], unsigned int table_size, in
4143

4244
void populate_gdt_entry(struct GDTEntry *entry,
4345
unsigned int base,
44-
unsigned int limit, // 20 bits
45-
unsigned char flags, // 4 bits
46-
unsigned char access_byte
46+
unsigned int limit, // 32 bit with 4k granularity
47+
unsigned char access_byte,
48+
int is_32_bit_selector
4749
);

src/kernel/process/allocation.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,16 +158,17 @@ int process_create(unsigned int ppid, int argc, char *argv[]) {
158158
// Application Code Segment Selector
159159
populate_gdt_entry(
160160
&gdt_table[idt_cs_entry],
161-
memory_location, memory_location+memory_size-1,
162-
0b0100, // 32-bit protected mode
163-
0x9a);
161+
memory_location, memory_size-1,
162+
0x9a,
163+
GDT_ENTRY_FLAG_32_BIT_SELECTOR // 32-bit protected mode
164+
);
164165
// Application Data Segment Selector
165166
populate_gdt_entry(
166167
&gdt_table[idt_ds_entry],
167-
memory_location, memory_location+memory_size-1,
168-
0b0100, // 32-bit protected mode
169-
0x92);
170-
168+
memory_location, memory_size-1,
169+
0x92,
170+
GDT_ENTRY_FLAG_32_BIT_SELECTOR // 32-bit protected mode
171+
);
171172

172173
// update process state
173174
process->ppid = ppid;

src/memmgr/tables/gdt.c

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,20 @@ int get_gdt_baseaddress(struct GDTEntry gdt_table[], unsigned int table_size, in
1717

1818
void populate_gdt_entry(struct GDTEntry *entry,
1919
unsigned int base,
20-
unsigned int limit, // 20 bits
21-
unsigned char flags, // 4 bits
22-
unsigned char access_byte
20+
unsigned int limit, // 32 bit with 4k granularity
21+
unsigned char access_byte,
22+
int is_32_bit_selector
2323
) {
24+
const int is_4k_granularity = 1;
25+
26+
char flags = 0b0000; // 4 bits
27+
if(is_4k_granularity) flags |= 0b1000;
28+
if(is_32_bit_selector) flags |= 0b0100;
29+
30+
if(is_4k_granularity) {
31+
limit>>=12;
32+
}
33+
2434
entry->base0 = (base&0x0000FFFF);
2535
entry->base1 = (base&0x00FF0000)>>16;
2636
entry->base2 = (base&0xFF000000)>>24;
@@ -46,38 +56,44 @@ void populate_gdt_table(
4656
// NULL selector
4757
populate_gdt_entry(
4858
&gdt_table[GDT_STD_SELECTOR_NULL],
49-
0,0,0,0);
59+
0,0,0,
60+
!GDT_ENTRY_FLAG_32_BIT_SELECTOR);
5061
// Kernel Code Segment Selector
5162
populate_gdt_entry(
5263
&gdt_table[GDT_STD_SELECTOR_KERNEL_CS],
53-
MEMORY_KERNEL_LOCATION, MEMORY_KERNEL_LOCATION+MEMORY_KERNEL_SIZE-1,
54-
0b0100, // 32-bit protected mode
55-
0x9a);
64+
MEMORY_KERNEL_LOCATION, MEMORY_KERNEL_SIZE-1,
65+
0x9a,
66+
GDT_ENTRY_FLAG_32_BIT_SELECTOR // 32-bit protected mode
67+
);
5668
// Kernel Data Segment Selector
5769
populate_gdt_entry(
5870
&gdt_table[GDT_STD_SELECTOR_KERNEL_DS],
59-
MEMORY_KERNEL_LOCATION, MEMORY_KERNEL_LOCATION+MEMORY_KERNEL_SIZE-1,
60-
0b0100, // 32-bit protected mode
61-
0x92);
71+
MEMORY_KERNEL_LOCATION, MEMORY_KERNEL_SIZE-1,
72+
0x92,
73+
GDT_ENTRY_FLAG_32_BIT_SELECTOR // 32-bit protected mode
74+
);
6275
// Absolute Code Segment Selector
6376
populate_gdt_entry(
6477
&gdt_table[GDT_STD_SELECTOR_ABS16_CS],
6578
0, 0xfffff,
66-
0b0000, // 16-bit protected mode
67-
0x9a);
79+
0x9a,
80+
!GDT_ENTRY_FLAG_32_BIT_SELECTOR // 16-bit protected mode
81+
);
6882
// Absolute Data Segment Selector
6983
populate_gdt_entry(
7084
&gdt_table[GDT_STD_SELECTOR_ABS16_DS],
7185
0, 0xfffff,
72-
0b0000, // 16-bit protected mode
73-
0x92);
86+
0x92,
87+
!GDT_ENTRY_FLAG_32_BIT_SELECTOR // 16-bit protected mode
88+
);
7489
// NOT USING &gdt_table[5] for now.
7590
// Absolute Data Segment Selector
7691
populate_gdt_entry(
7792
&gdt_table[GDT_STD_SELECTOR_ABS32_DS],
7893
0, 0xffffffff,
79-
0b0100, // 32-bit protected mode
80-
0x92);
94+
0x92,
95+
GDT_ENTRY_FLAG_32_BIT_SELECTOR // 32-bit protected mode
96+
);
8197
// Ensure GDT_STD_SIZE = last sector entry+1
8298

8399
gdtr->base_address = ds_fix+(int)gdt_table;

0 commit comments

Comments
 (0)