Skip to content

Commit d5f4f2f

Browse files
committed
feat(os): refine kernel memory management, ACPI power state logic, and window manager rendering pipeline
1 parent 9c4e287 commit d5f4f2f

20 files changed

Lines changed: 937 additions & 856 deletions

File tree

include/drivers/acpi/acpi.h

Lines changed: 112 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,114 @@
1-
#pragma once
2-
#include <stdint.h>
3-
4-
// ACPI RSDP (Root System Description Pointer) structures
5-
struct AcpiRsdp
6-
{
7-
char signature[8]; // "RSD PTR "
8-
uint8_t checksum;
9-
char oem_id[6];
10-
uint8_t revision; // 0 = ACPI 1.0, 2 = ACPI 2.0+
11-
uint32_t rsdt_address; // Physical address of RSDT
12-
} __attribute__((packed));
13-
14-
struct AcpiRsdp20
15-
{
16-
AcpiRsdp base;
17-
uint32_t length;
18-
uint64_t xsdt_address; // Physical address of XSDT (64-bit)
19-
uint8_t extended_checksum;
20-
uint8_t reserved[3];
21-
} __attribute__((packed));
22-
23-
// ACPI SDT Header (common to all tables)
24-
struct AcpiSdtHeader
25-
{
26-
char signature[4];
27-
uint32_t length;
28-
uint8_t revision;
29-
uint8_t checksum;
30-
char oem_id[6];
31-
char oem_table_id[8];
32-
uint32_t oem_revision;
33-
uint32_t creator_id;
34-
uint32_t creator_revision;
35-
} __attribute__((packed));
36-
37-
// ACPI FADT (Fixed ACPI Description Table) - partial, we only need power management fields
38-
struct AcpiFadt
39-
{
40-
AcpiSdtHeader header;
41-
uint32_t firmware_ctrl;
42-
uint32_t dsdt;
43-
uint8_t reserved1;
44-
uint8_t preferred_pm_profile;
45-
uint16_t sci_int;
46-
uint32_t smi_cmd;
47-
uint8_t acpi_enable;
48-
uint8_t acpi_disable;
1+
#pragma once
2+
#include <stdint.h>
3+
4+
// ACPI RSDP (Root System Description Pointer) structures
5+
struct AcpiRsdp
6+
{
7+
char signature[8]; // "RSD PTR "
8+
uint8_t checksum;
9+
char oem_id[6];
10+
uint8_t revision; // 0 = ACPI 1.0, 2 = ACPI 2.0+
11+
uint32_t rsdt_address; // Physical address of RSDT
12+
} __attribute__((packed));
13+
14+
struct AcpiRsdp20
15+
{
16+
AcpiRsdp base;
17+
uint32_t length;
18+
uint64_t xsdt_address; // Physical address of XSDT (64-bit)
19+
uint8_t extended_checksum;
20+
uint8_t reserved[3];
21+
} __attribute__((packed));
22+
23+
// ACPI SDT Header (common to all tables)
24+
struct AcpiSdtHeader
25+
{
26+
char signature[4];
27+
uint32_t length;
28+
uint8_t revision;
29+
uint8_t checksum;
30+
char oem_id[6];
31+
char oem_table_id[8];
32+
uint32_t oem_revision;
33+
uint32_t creator_id;
34+
uint32_t creator_revision;
35+
} __attribute__((packed));
36+
37+
struct AcpiGas
38+
{
39+
uint8_t address_space;
40+
uint8_t bit_width;
41+
uint8_t bit_offset;
42+
uint8_t access_size;
43+
uint64_t address;
44+
} __attribute__((packed));
45+
46+
// ACPI FADT (Fixed ACPI Description Table)
47+
struct AcpiFadt
48+
{
49+
AcpiSdtHeader header;
50+
uint32_t firmware_ctrl;
51+
uint32_t dsdt;
52+
uint8_t reserved1;
53+
uint8_t preferred_pm_profile;
54+
uint16_t sci_int;
55+
uint32_t smi_cmd;
56+
uint8_t acpi_enable;
57+
uint8_t acpi_disable;
4958
uint8_t s4_firmware_req;
50-
uint8_t pstate_cnt;
51-
uint32_t pm1a_evt_blk;
52-
uint32_t pm1b_evt_blk;
53-
uint32_t pm1a_cnt_blk; // PM1a Control Block (we need this for shutdown)
54-
uint32_t pm1b_cnt_blk; // PM1b Control Block (optional)
55-
uint32_t pm2_cnt_blk;
56-
uint32_t pm_tmr_blk;
57-
uint32_t gpe0_blk;
58-
uint32_t gpe1_blk;
59-
uint8_t pm1_evt_len;
60-
uint8_t pm1_cnt_len;
61-
uint8_t pm2_cnt_len;
62-
uint8_t pm_tmr_len;
63-
// ... more fields we don't need
64-
} __attribute__((packed));
65-
66-
// ACPI MCFG structure (PCI PCIe MMIO Configuration)
67-
struct AcpiMcfgEntry
68-
{
69-
uint64_t base_address;
70-
uint16_t pci_segment;
71-
uint8_t start_bus;
72-
uint8_t end_bus;
73-
uint32_t reserved;
74-
} __attribute__((packed));
75-
76-
struct AcpiMcfg
77-
{
78-
AcpiSdtHeader header;
79-
uint64_t reserved;
59+
uint8_t pstate_cnt;
60+
uint32_t pm1a_evt_blk;
61+
uint32_t pm1b_evt_blk;
62+
uint32_t pm1a_cnt_blk;
63+
uint32_t pm1b_cnt_blk;
64+
uint32_t pm2_cnt_blk;
65+
uint32_t pm_tmr_blk;
66+
uint32_t gpe0_blk;
67+
uint32_t gpe1_blk;
68+
uint8_t pm1_evt_len;
69+
uint8_t pm1_cnt_len;
70+
uint8_t pm2_cnt_len;
71+
uint8_t pm_tmr_len;
72+
uint8_t gpe0_blk_len;
73+
uint8_t gpe1_blk_len;
74+
uint8_t gpe1_base;
75+
uint8_t cstate_cnt;
76+
uint16_t lvl2_lat;
77+
uint16_t lvl3_lat;
78+
uint16_t flush_size;
79+
uint16_t flush_stride;
80+
uint8_t duty_offset;
81+
uint8_t duty_width;
82+
uint8_t day_alrm;
83+
uint8_t mon_alrm;
84+
uint8_t century;
85+
uint16_t iapc_boot_arch;
86+
uint8_t reserved2;
87+
uint32_t flags;
88+
AcpiGas reset_reg;
89+
uint8_t reset_value;
90+
} __attribute__((packed));
91+
92+
// ACPI MCFG structure (PCI PCIe MMIO Configuration)
93+
struct AcpiMcfgEntry
94+
{
95+
uint64_t base_address;
96+
uint16_t pci_segment;
97+
uint8_t start_bus;
98+
uint8_t end_bus;
99+
uint32_t reserved;
100+
} __attribute__((packed));
101+
102+
struct AcpiMcfg
103+
{
104+
AcpiSdtHeader header;
105+
uint64_t reserved;
80106
AcpiMcfgEntry entries[1];
81-
} __attribute__((packed));
82-
83-
// ACPI functions
84-
void acpi_init();
85-
void *acpi_find_table(const char *signature);
86-
bool acpi_poweroff();
87-
bool acpi_is_available();
107+
} __attribute__((packed));
108+
109+
// ACPI functions
110+
void acpi_init();
111+
void *acpi_find_table(const char *signature);
112+
bool acpi_reboot();
113+
bool acpi_poweroff();
114+
bool acpi_is_available();

include/uapi/gui.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ typedef struct WindowEntry
6868
{
6969
volatile int shm_id;
7070
volatile int x, y, w, h;
71+
volatile uint32_t position_serial;
7172
volatile int restore_x, restore_y, restore_w, restore_h;
7273
volatile int buffer_w, buffer_h;
7374
volatile int content_w, content_h;

include/uapi/syscalls.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181
#define SYS_DISPLAY_ATOMIC_COMMIT 260
8282
#define SYS_DISPLAY_BUFFER_SET_WM_ACCESS 261
8383
#define SYS_DISPLAY_SURFACE_IMPORT 262
84+
#define SYS_SHM_GET_OWNER 263
8485

8586
#define SYS_SIGACTION 13
8687
#define SYS_SIGRETURN 15

src/drivers/acpi/acpi.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ static uint16_t slp_typa = 0; // Sleep type value for S5
1414
static uint16_t slp_typb = 0; // Sleep type value for S5 (optional)
1515
static uint32_t smi_cmd_port = 0; // SMI command port
1616
static uint8_t acpi_enable_val = 0; // Value to write to enable ACPI
17+
static AcpiGas g_reset_reg = {}; // ACPI Reset Register
18+
static uint8_t g_reset_value = 0; // ACPI Reset Value
1719
static bool g_rsdp_logged = false;
1820

21+
1922
// Sleep enable bit
2023
#define ACPI_SLP_EN (1 << 13)
2124

@@ -269,14 +272,16 @@ void acpi_init()
269272
pm1b_cnt = fadt->pm1b_cnt_blk;
270273
smi_cmd_port = fadt->smi_cmd;
271274
acpi_enable_val = fadt->acpi_enable;
275+
g_reset_reg = fadt->reset_reg;
276+
g_reset_value = fadt->reset_value;
272277

273278
// Parse DSDT to find S5 sleep type
274279
if (fadt->dsdt) {
275280
find_s5_in_dsdt(fadt->dsdt);
276281
}
277282

278283
acpi_available = true;
279-
DEBUG_SUCCESS("ACPI ready (PM1a=0x%x)", pm1a_cnt);
284+
DEBUG_SUCCESS("ACPI ready (PM1a=0x%x, Reset=0x%lx)", pm1a_cnt, g_reset_reg.address);
280285
return;
281286
}
282287
}
@@ -292,6 +297,30 @@ bool acpi_is_available()
292297
// SCI_EN bit in PM1_CNT register
293298
#define ACPI_SCI_EN (1 << 0)
294299

300+
bool acpi_reboot()
301+
{
302+
if (!acpi_available || g_reset_reg.address == 0)
303+
return false;
304+
305+
DEBUG_INFO("acpi: attempting hardware reset via 0x%lx...", g_reset_reg.address);
306+
asm volatile("cli");
307+
308+
if (g_reset_reg.address_space == 1) { // System I/O
309+
outb((uint16_t)g_reset_reg.address, g_reset_value);
310+
} else if (g_reset_reg.address_space == 0) { // System Memory
311+
// We'd need to map this if it's not in HHDM or already mapped
312+
// For simplicity, we assume HHDM or identity for now if low enough
313+
if (g_reset_reg.address < 0x100000000ULL) {
314+
uint8_t *ptr = reinterpret_cast<uint8_t *>(vmm_phys_to_virt(g_reset_reg.address));
315+
*ptr = g_reset_value;
316+
}
317+
}
318+
319+
for (volatile int i = 0; i < 1000000; i++)
320+
;
321+
return false;
322+
}
323+
295324
bool acpi_poweroff()
296325
{
297326
DEBUG_INFO("acpi: starting shutdown sequence...");

src/drivers/video/display.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,8 +2915,8 @@ bool display_event_wait(DisplayEvent *out_event, bool block)
29152915
if (!out_event)
29162916
return false;
29172917

2918+
spinlock_acquire(&s_display_event_lock);
29182919
for (;;) {
2919-
spinlock_acquire(&s_display_event_lock);
29202920
if (display_pop_event_locked(out_event)) {
29212921
spinlock_release(&s_display_event_lock);
29222922
return true;
@@ -2926,9 +2926,9 @@ bool display_event_wait(DisplayEvent *out_event, bool block)
29262926
return false;
29272927
}
29282928

2929+
// scheduler_wait atomically drops the lock and sleeps.
2930+
// It returns with the lock re-acquired.
29292931
scheduler_wait(&s_display_event_wait_queue, &s_display_event_lock);
2930-
// scheduler_wait re-acquires the lock on wake, but display_event_wait
2931-
// doesn't need to release it again here since the loop will handle it.
29322932
}
29332933
}
29342934

src/kernel/core/syscall.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ extern "C" void signal_check(SyscallFrame *frame)
542542

543543
[[nodiscard]] static uint64_t sys_kill(uint64_t pid, int sig)
544544
{
545-
if (sig <= 0 || sig > 31)
545+
if (sig < 0 || sig > 31)
546546
return static_cast<uint64_t>(-1);
547547

548548
Process *current = process_get_current();
@@ -554,7 +554,9 @@ extern "C" void signal_check(SyscallFrame *frame)
554554
return static_cast<uint64_t>(-1);
555555
}
556556

557-
signal_send(target, sig);
557+
if (sig != 0) {
558+
signal_send(target, sig);
559+
}
558560
return 0;
559561
}
560562

@@ -2113,6 +2115,15 @@ extern "C" uint64_t syscall_handler(uint64_t syscall_num, uint64_t arg1, uint64_
21132115
spinlock_release(&g_shm_lock);
21142116
return size;
21152117
}
2118+
case SYS_SHM_GET_OWNER: {
2119+
int id = (int)arg1;
2120+
if (id < 0 || id >= 64)
2121+
return static_cast<uint64_t>(-1);
2122+
spinlock_acquire(&g_shm_lock);
2123+
uint32_t owner = g_shm_blocks[id].used ? g_shm_blocks[id].owner_pid : 0;
2124+
spinlock_release(&g_shm_lock);
2125+
return (uint64_t)owner;
2126+
}
21162127
case SYS_SHM_UNMAP:
21172128
return shm_unmap_from_process(process_get_current(), (int)arg1) ? 0 : static_cast<uint64_t>(-1);
21182129
default:

src/kernel/sched/scheduler.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,10 @@ void system_reboot()
506506
{
507507
shutdown_prepare(1, "System rebooting...");
508508

509+
asm volatile("wbinvd" ::: "memory");
510+
asm volatile("cli" ::: "memory");
511+
512+
acpi_reboot();
509513
reboot_via_keyboard_controller();
510514
reboot_via_pci_reset_control();
511515
reboot_via_triple_fault();
@@ -516,6 +520,9 @@ void system_poweroff()
516520
{
517521
shutdown_prepare(2, "System powering off...");
518522

523+
asm volatile("wbinvd" ::: "memory");
524+
asm volatile("cli" ::: "memory");
525+
519526
acpi_poweroff();
520527
DEBUG_WARN("Poweroff failed, halting CPU.");
521528
halt_forever();

0 commit comments

Comments
 (0)