Skip to content

Commit c9fcf64

Browse files
committed
arch: rebase onto ubm arch/. Builds, doesn't work.
1 parent c091937 commit c9fcf64

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+4354
-388
lines changed

arch/x86_64/acpi.c

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
#include <ng/debug.h>
2+
#include <ng/string.h>
3+
#include <ng/vmm.h>
4+
#include <ng/x86/acpi.h>
5+
#include <stdlib.h>
6+
#include <vec.h>
7+
8+
static acpi_rsdp_t *rsdp;
9+
static acpi_rsdt_t *rsdt;
10+
static vec(acpi_header_t *) mappings;
11+
12+
void acpi_init(acpi_rsdp_t *hw_rsdp) {
13+
rsdp = hw_rsdp;
14+
15+
rsdt = (acpi_rsdt_t *)(rsdp->rsdt_address + HW_MAP_BASE);
16+
int table_count = ((rsdt->header.length - sizeof(rsdt->header)) / 4);
17+
18+
for (int i = 0; i < table_count; i++) {
19+
vec_push(
20+
&mappings, (acpi_header_t *)(rsdt->table_ptr[i] + HW_MAP_BASE));
21+
}
22+
}
23+
24+
acpi_rsdt_t *acpi_rsdt(acpi_rsdp_t *rsdp) {
25+
if (!rsdt)
26+
acpi_init(rsdp);
27+
return rsdt;
28+
}
29+
30+
void *acpi_get_table(const char *table_id) {
31+
32+
vec_foreach(&mappings) {
33+
acpi_header_t *header = *it;
34+
if (memcmp(header->signature, table_id, 4) == 0) {
35+
return header;
36+
}
37+
}
38+
return nullptr;
39+
}
40+
41+
void acpi_print_rsdp(acpi_rsdp_t *rsdp) {
42+
printf("acpi rsdp @ %p {\n"
43+
"\tsignature: '%.8s'\n"
44+
"\tchecksum: %#04hhX\n"
45+
"\toem: '%.6s'\n"
46+
"\trevision: %hhi\n"
47+
"\trsdt: %#010X\n"
48+
"}\n",
49+
(void *)rsdp, rsdp->signature, rsdp->checksum, rsdp->oem_id,
50+
rsdp->revision, rsdp->rsdt_address);
51+
}
52+
53+
void acpi_print_header(acpi_header_t *header) {
54+
printf("\tsignature: '%.4s'\n"
55+
"\tlength: %u\n"
56+
"\trevision: %hhu\n"
57+
"\tchecksum: %#04hhX\n"
58+
"\toem: '%.6s'\n"
59+
"\toem table: '%.8s'\n"
60+
"\toem revision: %u\n"
61+
"\tcreator id: %u ('%.4s')\n"
62+
"\tcreator rev: %u\n",
63+
header->signature, header->length, header->revision, header->checksum,
64+
header->oem_id, header->oem_table_id, header->oem_revision,
65+
header->creator_id, (char *)&header->creator_id,
66+
header->creator_revision);
67+
}
68+
69+
void acpi_print_rsdt_tables(acpi_rsdt_t *rsdt) {
70+
printf("\ttables: [\n");
71+
vec_foreach(&mappings) {
72+
acpi_header_t *header = *it;
73+
printf("\t\t%.4s\n", header->signature);
74+
}
75+
printf("\t]\n");
76+
}
77+
78+
const char *madt_type_names[] = {
79+
[MADT_ENTRY_LAPIC] = "lapic",
80+
[MADT_ENTRY_IOAPIC] = "ioapic",
81+
[MADT_ENTRY_ISO] = "iso",
82+
[MADT_ENTRY_NMI] = "nmi",
83+
[MADT_ENTRY_LAPIC_ADDRESS] = "lapic address",
84+
};
85+
86+
void acpi_print_madt(acpi_madt_t *madt) {
87+
printf("\tlapic addr: %#010X\n"
88+
"\tflags: %#010X\n"
89+
"\tentries: [\n",
90+
madt->lapic_address, madt->flags);
91+
unsigned length = offsetof(acpi_madt_t, entries);
92+
while (length < madt->header.length) {
93+
acpi_madt_entry_t *entry = PTR_ADD(madt, length);
94+
printf("\t{\n"
95+
"\t\ttype: %s\n"
96+
"\t\tlength: %hhu\n",
97+
madt_type_names[entry->type], entry->length);
98+
switch (entry->type) {
99+
case MADT_ENTRY_LAPIC:
100+
printf("\t\tprocessor id: %hhu\n"
101+
"\t\tid: %hhu\n"
102+
"\t\tflags: %#010X\n",
103+
entry->lapic.processor_id, entry->lapic.id, entry->lapic.flags);
104+
break;
105+
case MADT_ENTRY_IOAPIC:
106+
printf("\t\tid: %hhu\n"
107+
"\t\taddress: %#010X\n"
108+
"\t\tinterrupt_base: %u\n",
109+
entry->ioapic.id, entry->ioapic.address,
110+
entry->ioapic.interrupt_base);
111+
break;
112+
case MADT_ENTRY_ISO:
113+
printf("\t\tbus source: %hhu\n"
114+
"\t\tirq source: %hhu\n"
115+
"\t\tglobal int: %u\n"
116+
"\t\tflags: %#06hX\n",
117+
entry->iso.bus_source, entry->iso.irq_source,
118+
entry->iso.global_system_interrupt, entry->iso.flags);
119+
break;
120+
case MADT_ENTRY_NMI:
121+
printf("\t\tprocessor id: %hhu\n"
122+
"\t\tflags: %#06hX\n"
123+
"\t\tlint number: %hhu\n",
124+
entry->nmi.processor_id, entry->nmi.flags,
125+
entry->nmi.LINT_number);
126+
break;
127+
case MADT_ENTRY_LAPIC_ADDRESS:
128+
printf(
129+
"\t\taddress: %#018lX\n", entry->lapic_address.address);
130+
break;
131+
default:
132+
printf("\t\t(unknown)\n");
133+
}
134+
printf("\t}\n");
135+
length += entry->length;
136+
}
137+
printf("\t]\n");
138+
}
139+
140+
void acpi_print_table(acpi_header_t *header) {
141+
printf("acpi %.4s @ %p {\n", header->signature, (void *)header);
142+
acpi_print_header(header);
143+
if (memcmp(header->signature, "APIC", 4) == 0) {
144+
acpi_print_madt((acpi_madt_t *)header);
145+
}
146+
if (memcmp(header->signature, "RSDT", 4) == 0) {
147+
acpi_print_rsdt_tables((acpi_rsdt_t *)header);
148+
}
149+
printf("}\n");
150+
}

arch/x86_64/asm.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#pragma once
2+
3+
#include "x86_64.h"
4+
5+
// clang-format off
6+
7+
.macro PUSH_ALL
8+
push %rax
9+
push %rcx
10+
push %rdx
11+
push %rbx
12+
push %rbp
13+
push %rsi
14+
push %rdi
15+
push %r8
16+
push %r9
17+
push %r10
18+
push %r11
19+
push %r12
20+
push %r13
21+
push %r14
22+
push %r15
23+
.endm
24+
25+
.macro POP_ALL
26+
pop %r15
27+
pop %r14
28+
pop %r13
29+
pop %r12
30+
pop %r11
31+
pop %r10
32+
pop %r9
33+
pop %r8
34+
pop %rdi
35+
pop %rsi
36+
pop %rbp
37+
pop %rbx
38+
pop %rdx
39+
pop %rcx
40+
pop %rax
41+
.endm
42+
43+
.macro PUSH_CALLEE_SAVED
44+
push %rbp
45+
push %rbx
46+
push %r12
47+
push %r13
48+
push %r14
49+
push %r15
50+
.endm
51+
52+
.macro POP_CALLEE_SAVED
53+
pop %r15
54+
pop %r14
55+
pop %r13
56+
pop %r12
57+
pop %rbx
58+
pop %rbp
59+
.endm

arch/x86_64/ata.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <ng/x86/cpu.h>
2+
3+
#define ATA_STATUS_ERR 0x01
4+
#define ATA_STATUS_DRQ 0x08
5+
#define ATA_STATUS_DF 0x20
6+
#define ATA_STATUS_READY 0x40
7+
#define ATA_STATUS_BUSY 0x80
8+
9+
static void wait_until_not_busy() {
10+
while (inb(0x1F7) & ATA_STATUS_BUSY) { }
11+
}
12+
13+
static void wait_until_not_ready() {
14+
while (!(inb(0x1F7) & ATA_STATUS_READY)) { }
15+
}
16+
17+
int read_sector(long lba, void *dest) {
18+
wait_until_not_busy();
19+
outb(0x1F6, 0xE0 | ((lba >> 24) & 0xF));
20+
outb(0x1F2, 1);
21+
outb(0x1F3, (uint8_t)lba);
22+
outb(0x1F4, (uint8_t)(lba >> 8));
23+
outb(0x1F5, (uint8_t)(lba >> 16));
24+
outb(0x1F7, 0x20); // Send the read command
25+
uint16_t *buffer = (uint16_t *)dest;
26+
27+
wait_until_not_busy();
28+
wait_until_not_ready();
29+
30+
for (int i = 0; i < 256; i++)
31+
buffer[i] = inw(0x1F0);
32+
33+
return 0;
34+
}
35+
36+
int write_sector(long lba, void *src) {
37+
wait_until_not_busy();
38+
outb(0x1F6, 0xE0 | ((lba >> 24) & 0xF));
39+
outb(0x1F2, 1);
40+
outb(0x1F3, (uint8_t)lba);
41+
outb(0x1F4, (uint8_t)(lba >> 8));
42+
outb(0x1F5, (uint8_t)(lba >> 16));
43+
outb(0x1F7, 0x30); // Send the write command
44+
uint16_t *buffer = (uint16_t *)src;
45+
46+
wait_until_not_busy();
47+
wait_until_not_ready();
48+
49+
for (int i = 0; i < 256; i++)
50+
outw(0x1F0, buffer[i]);
51+
52+
return 0;
53+
}

arch/x86_64/debug.c

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include "stdio.h"
2+
#include "x86_64.h"
3+
#include <setjmp.h>
4+
5+
void print_backtrace_raw(uintptr_t rbp, uintptr_t rip) {
6+
if (this_cpu->printing_backtrace) {
7+
printf("Faulted in backtrace\n");
8+
return;
9+
}
10+
11+
this_cpu->printing_backtrace = true;
12+
13+
printf("Backtrace:\n");
14+
15+
int frames = 0;
16+
17+
while (rbp && frames++ < 25) {
18+
printf(" frame ip: %lx\n", rip);
19+
20+
rip = *(uintptr_t *)(rbp + 8);
21+
rbp = *(uintptr_t *)rbp;
22+
}
23+
24+
this_cpu->printing_backtrace = false;
25+
}
26+
27+
void print_backtrace(frame_t *f) { print_backtrace_raw(f->rbp, f->rip); }
28+
29+
void backtrace_frame(frame_t *f) { print_backtrace_raw(f->rbp, f->rip); }
30+
31+
void backtrace_context(jmp_buf b) {
32+
print_backtrace_raw(b->__regs.bp, b->__regs.ip);
33+
}
34+
35+
void print_frame(frame_t *f) {
36+
printf("rax %16lx rbx %16lx rcx %16lx rdx %16lx\n", f->rax, f->rbx, f->rcx,
37+
f->rdx);
38+
printf("rsi %16lx rdi %16lx rbp %16lx rsp %16lx\n", f->rsi, f->rdi, f->rbp,
39+
f->rsp);
40+
printf(" r8 %16lx r9 %16lx r10 %16lx r11 %16lx\n", f->r8, f->r9, f->r10,
41+
f->r11);
42+
printf("r12 %16lx r13 %16lx r14 %16lx r15 %16lx\n", f->r12, f->r13, f->r14,
43+
f->r15);
44+
printf(" cs %16lx ss %16lx\n", f->cs, f->ss);
45+
printf("int %16lx err %16lx rip %16lx flg %16lx\n", f->int_no, f->err_code,
46+
f->rip, f->rflags);
47+
}

arch/x86_64/entry.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "limine.h"
2+
#include "ng/mem.h"
3+
#include "sys/cdefs.h"
4+
#include "x86_64.h"
5+
#include <ng/arch-2.h>
6+
7+
LIMINE_BASE_REVISION(1)
8+
9+
void kernel_main();
10+
11+
// The kernel entrypoint, called by the bootloader.
12+
// This function is set as the entry on the kernel ELF file.
13+
USED void kernel_entry() {
14+
init_bsp_gdt();
15+
init_idt();
16+
init_syscall();
17+
init_page_mmap();
18+
init_kmem_alloc();
19+
init_int_stacks();
20+
init_aps();
21+
22+
kernel_main();
23+
24+
halt_forever();
25+
}

0 commit comments

Comments
 (0)