Skip to content

Commit f39779e

Browse files
committedMar 20, 2025
i386: remove dependency on <sys/mman.h>
This defines memory-protection flags specifically for the GDT.
1 parent 96eafea commit f39779e

File tree

5 files changed

+19
-16
lines changed

5 files changed

+19
-16
lines changed
 

‎src/arch/i386/gdt.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#ifndef UKERNEL_ARCH_I386_GDT_H
88
# define UKERNEL_ARCH_I386_GDT_H
99

10-
# include <sys/mman.h>
1110
# include <sys/types.h>
1211
# include <stdint.h>
1312

@@ -70,6 +69,10 @@ typedef struct _TSS
7069

7170

7271
/* constants */
72+
# define GDT_PROT_READ 0x1
73+
# define GDT_PROT_WRITE 0x2
74+
# define GDT_PROT_EXEC 0x4
75+
7376
# define GDT_SYSTEM_TYPE_LDT 0x2
7477
# define GDT_SYSTEM_TYPE_TSS 0x9
7578

‎src/kernel/multiboot.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
/* GDT: 4GB flat memory setup */
3030
static const GDTTable _gdt_4gb[2] =
3131
{
32-
{ 0x00000000, 0xffffffff, PROT_READ | PROT_EXEC },
33-
{ 0x00000000, 0xffffffff, PROT_READ | PROT_WRITE }
32+
{ 0x00000000, 0xffffffff, GDT_PROT_READ | GDT_PROT_EXEC },
33+
{ 0x00000000, 0xffffffff, GDT_PROT_READ | GDT_PROT_WRITE }
3434
};
3535

3636
static const IDT _idt[] =

‎src/loader/gdt.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ int gdt_append(GDT * gdt, vaddr_t base, size_t size, unsigned int prot)
139139
errno = ENOMEM;
140140
return -1;
141141
}
142-
if(prot != PROT_READ
143-
&& prot != (PROT_READ | PROT_WRITE)
144-
&& prot != (PROT_READ | PROT_EXEC))
142+
if(prot != GDT_PROT_READ
143+
&& prot != (GDT_PROT_READ | GDT_PROT_WRITE)
144+
&& prot != (GDT_PROT_READ | GDT_PROT_EXEC))
145145
{
146146
errno = EPERM;
147147
return -1;
@@ -157,13 +157,13 @@ int gdt_append(GDT * gdt, vaddr_t base, size_t size, unsigned int prot)
157157
else
158158
limit = size - 1;
159159
/* access */
160-
if(prot == (PROT_READ | PROT_EXEC))
160+
if(prot == (GDT_PROT_READ | GDT_PROT_EXEC))
161161
/* code segment */
162162
access |= GDT_ACCESS_PROT_RW | GDT_ACCESS_PROT_X;
163-
else if(prot == (PROT_READ | PROT_WRITE))
163+
else if(prot == (GDT_PROT_READ | GDT_PROT_WRITE))
164164
/* data segment (read/write) */
165165
access |= GDT_ACCESS_PROT_RW;
166-
else if(prot == PROT_READ)
166+
else if(prot == GDT_PROT_READ)
167167
/* data segment (read-only) */
168168
access |= GDT_ACCESS_SET;
169169
return _gdt_append_entry(gdt, base, limit, access, flags);

‎src/loader/multiboot.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
/* GDT: 4GB flat memory setup */
3232
static const GDTTable _gdt_4gb[2] =
3333
{
34-
{ 0x00000000, 0xffffffff, PROT_READ | PROT_EXEC },
35-
{ 0x00000000, 0xffffffff, PROT_READ | PROT_WRITE }
34+
{ 0x00000000, 0xffffffff, GDT_PROT_READ | GDT_PROT_EXEC },
35+
{ 0x00000000, 0xffffffff, GDT_PROT_READ | GDT_PROT_WRITE }
3636
};
3737

3838

‎tests/gdt.c

+5-5
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@ int main(int argc, char * argv[])
5454
/* flat 4 GB */
5555
printf("Flat 4 GB:\n");
5656
gdt = gdt_init();
57-
gdt_append(gdt, 0x00000000, 0xffffffff, PROT_READ | PROT_EXEC);
58-
gdt_append(gdt, 0x00000000, 0xffffffff, PROT_READ | PROT_WRITE);
57+
gdt_append(gdt, 0x00000000, 0xffffffff, GDT_PROT_READ | GDT_PROT_EXEC);
58+
gdt_append(gdt, 0x00000000, 0xffffffff, GDT_PROT_READ | GDT_PROT_WRITE);
5959
gdt_append_system(gdt, &tss, sizeof(tss), GDT_SYSTEM_TYPE_TSS);
6060
gdt_apply(gdt);
6161

6262
/* 4 MB code + 4 MB data (read-write) + 4 MB data (read-only) */
6363
printf("\n4 MB (rx) + 4 MB (rw) + 4 MB (ro):\n");
6464
gdt = gdt_init();
65-
gdt_append(gdt, 0x00400000, 0x00400000, PROT_READ | PROT_EXEC);
66-
gdt_append(gdt, 0x00800000, 0x00400000, PROT_READ | PROT_WRITE);
67-
gdt_append(gdt, 0x00c00000, 0x00400000, PROT_READ);
65+
gdt_append(gdt, 0x00400000, 0x00400000, GDT_PROT_READ | GDT_PROT_EXEC);
66+
gdt_append(gdt, 0x00800000, 0x00400000, GDT_PROT_READ | GDT_PROT_WRITE);
67+
gdt_append(gdt, 0x00c00000, 0x00400000, GDT_PROT_READ);
6868
gdt_append_system(gdt, &tss, sizeof(tss), GDT_SYSTEM_TYPE_TSS);
6969
gdt_apply(gdt);
7070

0 commit comments

Comments
 (0)