Skip to content

Commit c5f9ff2

Browse files
committed
Added PMM feature, Added new types, Updated limine
1 parent c2e3675 commit c5f9ff2

File tree

35 files changed

+596
-190
lines changed

35 files changed

+596
-190
lines changed

arch/x86_64/boot/kernel.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
#include <phoenix/kernel.h>
1717
#include <phoenix/serial.h>
18-
#include <phoenix/vga.h>
18+
#include <phoenix/mem.h>
1919
#include <cpuid.h>
2020

2121
void kernel_main(void)

arch/x86_64/boot/stivale2/stivale2.c

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@
1616
#include <phoenix/kernel.h>
1717
#include <phoenix/stivale2.h>
1818
#include <phoenix/serial.h>
19-
#include <phoenix/pmm.h>
20-
#include <phoenix/vga.h>
2119
#include <stivale2.h>
2220
#include <stdint.h>
2321
#include <stddef.h>
2422

2523
/* Stack required by the stivale2 specifications */
26-
static uint8_t stack[4096];
24+
static u8 stack[4096];
2725

2826
/*
2927
static struct stivale2_header_tag_framebuffer framebuffer_tag = {
@@ -41,20 +39,20 @@ static struct stivale2_header_tag_smp smp_tag = {
4139
.tag = {
4240
.identifier = STIVALE2_HEADER_TAG_SMP_ID,
4341
.next = 0,
44-
//.next = (uint64_t)&framebuffer_tag,
42+
//.next = (u64)&framebuffer_tag,
4543
},
4644
.flags = 0
4745
};
4846
*/
4947

5048
/* Base Stivale2 header */
51-
__attribute__((section(".stivale2hdr"), used))
49+
SECTION(".stivale2hdr")
5250
struct stivale2_header stivale_hdr = {
53-
.entry_point = (uintptr_t)&init,
54-
.stack = (uintptr_t)stack + sizeof(stack),
51+
.entry_point = (uptr)&init,
52+
.stack = (uptr)stack + sizeof(stack),
5553
.flags = 0,
5654
.tags = 0,
57-
/*.tags = (uint64_t)&smp_tag, */
55+
/*.tags = (u64)&smp_tag, */
5856
};
5957

6058
void stivale2_print_fb_tag(struct stivale2_struct_tag_framebuffer* fb_tag)
@@ -94,7 +92,7 @@ void stivale2_print_smp_tag(struct stivale2_struct_tag_smp* smp_tag)
9492
void stivale2_print_memmap(struct stivale2_struct_tag_memmap* memmap_tag)
9593
{
9694
info("Memmap Entries: %d\n", memmap_tag->entries);
97-
for (uint64_t i = 0; i < memmap_tag->entries; i++) {
95+
for (u64 i = 0; i < memmap_tag->entries; i++) {
9896
struct stivale2_mmap_entry* current_entry = &memmap_tag->memmap[i];
9997
switch (current_entry->type) {
10098

@@ -140,7 +138,7 @@ void stivale2_print_memmap(struct stivale2_struct_tag_memmap* memmap_tag)
140138
info("\n");
141139
}
142140

143-
void* stivale2_get_tag(struct stivale2_struct* hdr, uint64_t id)
141+
void* stivale2_get_tag(struct stivale2_struct* hdr, u64 id)
144142
{
145143
struct stivale2_tag* current_tag = (void*)hdr->tags;
146144
for (;;) {
@@ -159,8 +157,8 @@ void* stivale2_get_tag(struct stivale2_struct* hdr, uint64_t id)
159157
}
160158
}
161159

162-
struct usable_memory_hdr
163-
stivale2_get_usable_memory(struct stivale2_struct* hdr)
160+
struct free_memory_hdr
161+
stivale2_get_free_memory(struct stivale2_struct* hdr)
164162
{
165163
/* Get memmap tag */
166164
struct stivale2_struct_tag_memmap* memmap_tag;
@@ -172,39 +170,46 @@ stivale2_get_usable_memory(struct stivale2_struct* hdr)
172170
panic("No stivale2 memmap tag has been found !\n");
173171
}
174172

175-
/* Get Usable Entries */
176-
struct usable_memory_hdr usable_hdr;
177-
struct usable_memory entry;
178-
usable_hdr.entries = 0;
179-
uint64_t i;
180-
for (i = 0; i < memmap_tag->entries; i++) {
173+
/* Get free entries */
174+
struct free_memory_hdr free_hdr;
175+
struct free_memory entry;
176+
uptr highest_memory = 0;
177+
free_hdr.entries = 0;
178+
179+
for (u64 i = 0; i < memmap_tag->entries; i++) {
181180
struct stivale2_mmap_entry* current_entry = &memmap_tag->memmap[i];
182181

183-
usable_hdr.total_memory += entry.length;
182+
free_hdr.total_memory += entry.length;
184183

185184
if (current_entry->type == STIVALE2_MMAP_USABLE ||
186185
current_entry->type == STIVALE2_MMAP_KERNEL_AND_MODULES ||
187-
current_entry->type == STIVALE2_MMAP_BOOTLOADER_RECLAIMABLE) {
186+
current_entry->type == STIVALE2_MMAP_BOOTLOADER_RECLAIMABLE) {
188187

189188
/* Found an Usable Entry */
190189
entry.base = current_entry->base;
191190
entry.length = current_entry->length;
192-
entry.type = current_entry->type;
193-
usable_hdr.segments[usable_hdr.entries] = entry;
191+
entry.type = current_entry->type;
192+
free_hdr.segments[free_hdr.entries] = entry;
193+
194+
debug("[STIVALE2_MEMMAP] Stored a free memory entry\n");
195+
196+
highest_memory = entry.base + entry.length;
194197

195-
debug("[STIVALE2_MEMMAP] Found an usable entry\n");
198+
/* Store the highest free memory */
199+
if (highest_memory > free_hdr.highest_memory)
200+
free_hdr.highest_memory = highest_memory;
196201

197-
usable_hdr.usable_memory += entry.length;
198-
usable_hdr.entries++;
202+
free_hdr.free_memory += entry.length;
203+
free_hdr.entries++;
199204
}
200205
}
201206

202207
/* Panic if no entries are found */
203-
if (usable_hdr.entries <= 0) {
204-
panic("No usable memory has been found !\n");
208+
if (free_hdr.entries <= 0) {
209+
panic("No free memory has been found !\n");
205210
}
206211

207-
return usable_hdr;
212+
return free_hdr;
208213
}
209214

210215
char* stivale2_get_cmdline(struct stivale2_struct* hdr)

drivers/serial.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
#include <phoenix/io.h>
2020
#include <stddef.h>
2121

22-
static int serial_port_check(const uint16_t serial_port);
23-
static uint8_t serial_initialized = 0;
22+
static int serial_port_check(const u16 serial_port);
23+
static u8 serial_initialized = 0;
2424

2525
/* Initialize serial connexion */
26-
int serial_init(const uint16_t serial_port, uint16_t baud_divisor)
26+
int serial_init(const u16 serial_port, uint16_t baud_divisor)
2727
{
2828
/* Check is the serial port is supported */
2929
if (serial_port_check(serial_port)) return 1;
@@ -50,7 +50,7 @@ int serial_init(const uint16_t serial_port, uint16_t baud_divisor)
5050
}
5151

5252
/* Check if a serial port is valid */
53-
static int serial_port_check(const uint16_t serial_port)
53+
static int serial_port_check(const u16 serial_port)
5454
{
5555
if (serial_port == SERIAL_COM1 || serial_port == SERIAL_COM2 ||
5656
serial_port == SERIAL_COM3 || serial_port == SERIAL_COM4) {
@@ -60,7 +60,7 @@ static int serial_port_check(const uint16_t serial_port)
6060
}
6161

6262
/* Check if we can receive data */
63-
int serial_received(const uint16_t serial_port)
63+
int serial_received(const u16 serial_port)
6464
{
6565
/* Check is the serial port is supported */
6666
if (!serial_initialized || serial_port_check(serial_port)) return -1;
@@ -69,7 +69,7 @@ int serial_received(const uint16_t serial_port)
6969
}
7070

7171
/* Read data thought serial */
72-
char serial_read(const uint16_t serial_port)
72+
char serial_read(const u16 serial_port)
7373
{
7474
/* Check is the serial port is supported */
7575
if (!serial_initialized || serial_port_check(serial_port)) return -1;
@@ -80,7 +80,7 @@ char serial_read(const uint16_t serial_port)
8080
}
8181

8282
/* Read a string thought serial */
83-
void serial_readstring(const uint16_t serial_port, char* buff, size_t size)
83+
void serial_readstring(const u16 serial_port, char* buff, size_t size)
8484
{
8585
size_t i = 0;
8686
char c = serial_read(serial_port);
@@ -98,7 +98,7 @@ void serial_readstring(const uint16_t serial_port, char* buff, size_t size)
9898
}
9999

100100
/* Check the end of the received data */
101-
static int serial_is_transmit_empty(const uint16_t serial_port)
101+
static int serial_is_transmit_empty(const u16 serial_port)
102102
{
103103
/* Check is the serial port is supported */
104104
if (!serial_initialized || serial_port_check(serial_port)) return -1;
@@ -107,7 +107,7 @@ static int serial_is_transmit_empty(const uint16_t serial_port)
107107
}
108108

109109
/* Write a char thought serial */
110-
void serial_write(const uint16_t serial_port, const char c)
110+
void serial_write(const u16 serial_port, const char c)
111111
{
112112
/* Check is the serial port is supported */
113113
if (!serial_initialized || serial_port_check(serial_port)) return;
@@ -118,7 +118,7 @@ void serial_write(const uint16_t serial_port, const char c)
118118
}
119119

120120
/* Write a string thought serial */
121-
void serial_writestring(const uint16_t serial_port, const char* string)
121+
void serial_writestring(const u16 serial_port, const char* string)
122122
{
123123
while (*string) {
124124
serial_write(serial_port, *(string++));

drivers/vga.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1515
*/
1616
#include <phoenix/serial.h>
17+
#include <phoenix/types.h>
1718
#include <phoenix/mem.h>
1819
#include <phoenix/vga.h>
1920
#include <phoenix/io.h>
@@ -24,12 +25,12 @@
2425
static const size_t VGA_WIDTH = 80;
2526
static const size_t VGA_HEIGHT = 25;
2627
static const size_t TAB_LENGTH = 4;
27-
static uint16_t* const VGA_MEMORY = (uint16_t*) 0xB8000;
28+
static u16* const VGA_MEMORY = (u16*) 0xB8000;
2829

2930
static size_t vga_row;
3031
static size_t vga_column;
31-
static uint8_t vga_color;
32-
static uint16_t* vga_buffer = VGA_MEMORY;
32+
static u8 vga_color;
33+
static u16* vga_buffer = VGA_MEMORY;
3334

3435
void vga_cursor_enable(void)
3536
{
@@ -45,23 +46,23 @@ void vga_cursor_disable(void)
4546
outb(0x3d5, 0x20);
4647
}
4748

48-
uint16_t vga_cursor_get_pos(void)
49+
u16 vga_cursor_get_pos(void)
4950
{
50-
uint16_t pos = 0;
51+
u16 pos = 0;
5152
outb(0x3d4, 0x0f);
5253
pos |= inb(0x3d5);
5354
outb(0x3d4, 0x0e);
54-
pos |= ((uint16_t) inb(0x3d5)) << 8;
55+
pos |= ((u16) inb(0x3d5)) << 8;
5556
return pos;
5657
}
5758

58-
void vga_cursor_set_pos(uint8_t x, uint8_t y)
59+
void vga_cursor_set_pos(u8 x, uint8_t y)
5960
{
60-
uint16_t pos = y * VGA_WIDTH + x;
61+
u16 pos = y * VGA_WIDTH + x;
6162
outb(0x3d4, 0x0f);
62-
outb(0x3d5, (uint8_t) (pos & 0xff));
63+
outb(0x3d5, (u8) (pos & 0xff));
6364
outb(0x3d4, 0x0e);
64-
outb(0x3d5, (uint8_t) ((pos >> 8) & 0xff));
65+
outb(0x3d5, (u8) ((pos >> 8) & 0xff));
6566
}
6667

6768
void vga_clear(void)
@@ -90,12 +91,12 @@ void vga_init(void)
9091
debug("[VGA] Initialized\n");
9192
}
9293

93-
void vga_setcolor(uint8_t color)
94+
void vga_setcolor(u8 color)
9495
{
9596
vga_color = color;
9697
}
9798

98-
void vga_putentryat(unsigned char c, uint8_t color, size_t x, size_t y)
99+
void vga_putentryat(unsigned char c, u8 color, size_t x, size_t y)
99100
{
100101
const size_t index = y * VGA_WIDTH + x;
101102
vga_buffer[index] = vga_entry(c, color);

include/phoenix/boot.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright © 2021 Guillot Tony <tony.guillot@protonmail.com>
3+
* This program is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* This program is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
15+
*/
16+
#ifndef _BOOT_H_
17+
#define _BOOT_H_
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
22+
23+
#include <phoenix/stivale2.h>
24+
#include <phoenix/types.h>
25+
#include <stivale2.h>
26+
#include <stdint.h>
27+
28+
#define LIMIT_SEGMENTS 64
29+
30+
struct free_memory {
31+
u64 base;
32+
u64 length;
33+
u32 type;
34+
} PACKED;
35+
36+
struct free_memory_hdr {
37+
u64 entries;
38+
u64 free_memory;
39+
u64 total_memory;
40+
uptr highest_memory;
41+
struct free_memory segments[LIMIT_SEGMENTS];
42+
} PACKED;
43+
44+
#ifdef __cplusplus
45+
}
46+
#endif
47+
48+
#endif /* _BOOT_H_ */

include/phoenix/gdt.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,23 @@
2020
extern "C" {
2121
#endif
2222

23+
#include <phoenix/types.h>
2324
#include <stdint.h>
2425

2526
struct gdt_pointer {
26-
uint16_t limit;
27-
uint64_t base;
28-
} __attribute__((packed));
27+
u16 limit;
28+
u64 base;
29+
} PACKED;
2930

3031
struct gdt_descriptor {
31-
uint16_t limit_low; /* The low 16 bits of the limit of the segment */
32-
uint16_t base_low; /* The low 16 bits of the base of the segment */
33-
uint8_t base_mid; /* The middle 8 bits of the base of the segment */
34-
35-
uint8_t access; /* The 8 bits descriptor type */
36-
uint8_t limit_flags; /* The high 4 bits of the limit and The 4 bits flags */
37-
uint8_t base_high; /* The high 8 bits of the base of the segment */
38-
} __attribute__((packed));
32+
u16 limit_low; /* The low 16 bits of the limit of the segment */
33+
u16 base_low; /* The low 16 bits of the base of the segment */
34+
u8 base_mid; /* The middle 8 bits of the base of the segment */
35+
36+
u8 access; /* The 8 bits descriptor type */
37+
u8 limit_flags; /* The high 4 bits of the limit and The 4 bits flags */
38+
u8 base_high; /* The high 8 bits of the base of the segment */
39+
} PACKED;
3940

4041
void gdt_init(void);
4142

0 commit comments

Comments
 (0)