v2 initial commit
This commit is contained in:
parent
2c97dba9f7
commit
d2ef885e8d
118 changed files with 2420 additions and 9320 deletions
21
amd64/arch/msr.S
Normal file
21
amd64/arch/msr.S
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
.intel_syntax noprefix
|
||||
|
||||
.global rdmsr
|
||||
rdmsr:
|
||||
mov ecx, edi
|
||||
rdmsr
|
||||
shl rdx, 0x20
|
||||
or rax, rdx
|
||||
ret
|
||||
|
||||
.global wrmsr
|
||||
wrmsr:
|
||||
mov rdx, rsi
|
||||
shr rdx, 0x20
|
||||
mov eax, esi
|
||||
mov ecx, edi
|
||||
wrmsr
|
||||
|
||||
ret
|
||||
|
||||
20
amd64/arch/page.c
Normal file
20
amd64/arch/page.c
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/types.h>
|
||||
#include <kernel/page.h>
|
||||
#include <kernel/mem.h>
|
||||
|
||||
#include <arch/page.h>
|
||||
#include "reg.h"
|
||||
|
||||
void pgd_load(pgd_t *pgd)
|
||||
{
|
||||
uintptr_t i = (uintptr_t)pgd;
|
||||
|
||||
cr3_write(TO_PHYS(i));
|
||||
}
|
||||
|
||||
pgd_t *pgd_get()
|
||||
{
|
||||
uintptr_t ret = cr3_read() | hhdm_start;
|
||||
return (void *)ret;
|
||||
}
|
||||
12
amd64/arch/reg.S
Normal file
12
amd64/arch/reg.S
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
.intel_syntax noprefix
|
||||
|
||||
.global cr3_write
|
||||
cr3_write:
|
||||
mov cr3, rdi
|
||||
ret
|
||||
|
||||
.global cr3_read
|
||||
cr3_read:
|
||||
mov rax, cr3
|
||||
ret
|
||||
|
||||
10
amd64/arch/reg.h
Normal file
10
amd64/arch/reg.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _REG_H_
|
||||
#define _REG_H_
|
||||
|
||||
#include <kernel/types.h>
|
||||
|
||||
void cr3_write();
|
||||
u64 cr3_read();
|
||||
|
||||
#endif /* _REG_H_ */
|
||||
109
amd64/drivers/apic.c
Normal file
109
amd64/drivers/apic.c
Normal file
|
|
@ -0,0 +1,109 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/mem.h>
|
||||
#include <drivers/apic.h>
|
||||
#include <arch/msr.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
static uintptr_t ioapic_addr = 0xFEC00000;
|
||||
static uintptr_t lapic_addr = 0xFEE00000;
|
||||
static uint8_t lapic_id = 0;
|
||||
|
||||
static struct ioapic {
|
||||
uint32_t reg;
|
||||
uint32_t pad[3];
|
||||
uint32_t data;
|
||||
} volatile *ioapic;
|
||||
|
||||
uint32_t ioapic_read(uint32_t reg)
|
||||
{
|
||||
ioapic->reg = reg;
|
||||
return ioapic->data;
|
||||
}
|
||||
|
||||
void ioapic_write(uint32_t reg, uint32_t value)
|
||||
{
|
||||
ioapic->reg = reg;
|
||||
ioapic->data = value;
|
||||
}
|
||||
|
||||
void ioapic_redirect_insert(size_t irq, size_t isr, size_t low_active, size_t level_trigger)
|
||||
{
|
||||
/* Set the interrupt redirection entry */
|
||||
ioapic_write(0x10 + 2 * irq, (level_trigger << 15) | (low_active << 13) | isr);
|
||||
ioapic_write(0x10 + 2 * irq + 1, lapic_id << 24);
|
||||
}
|
||||
|
||||
uint32_t lapic_read(uintptr_t addr, unsigned int regoffset)
|
||||
{
|
||||
volatile uint32_t *lapic = (volatile uint32_t *)addr;
|
||||
return lapic[regoffset >> 2];
|
||||
}
|
||||
|
||||
uint32_t lapic_write(uintptr_t addr, unsigned int regoffset, uint32_t value)
|
||||
{
|
||||
volatile uint32_t *lapic = (volatile uint32_t *)addr;
|
||||
lapic[regoffset >> 2] = value;
|
||||
return lapic[regoffset >> 2];
|
||||
}
|
||||
|
||||
void apic_eoi()
|
||||
{
|
||||
lapic_write(lapic_addr, 0xB0, 0);
|
||||
}
|
||||
|
||||
void lapic_eoi()
|
||||
{
|
||||
lapic_write(lapic_addr, LAPIC_EOI, 0);
|
||||
}
|
||||
|
||||
void lapic_enable()
|
||||
{
|
||||
/* Set spurious interrupt vector to 0xFF and enable the LAPIC */
|
||||
lapic_write(lapic_addr, 0xF0, 0x1FF);
|
||||
}
|
||||
|
||||
uint8_t lapic_idno()
|
||||
{
|
||||
return lapic_read(lapic_addr, LAPIC_ID) >> 24;
|
||||
}
|
||||
|
||||
void apic_init()
|
||||
{
|
||||
ioapic_addr += hhdm_start;
|
||||
lapic_addr += hhdm_start;
|
||||
|
||||
ioapic = (void *)ioapic_addr;
|
||||
|
||||
uint32_t r = ioapic_read(0);
|
||||
r |= 0x04000000;
|
||||
ioapic_write(0, r);
|
||||
|
||||
lapic_id = lapic_read(lapic_addr, 0x20);
|
||||
ioapic_redirect_insert(4, 0x24, 0, 0);
|
||||
|
||||
lapic_enable();
|
||||
|
||||
uint64_t msr = rdmsr(MSR_IA32_APIC_BASE);
|
||||
msr |= 1 << 11;
|
||||
wrmsr(MSR_IA32_APIC_BASE, msr);
|
||||
|
||||
lapic_eoi();
|
||||
|
||||
krdy("APIC initialized\n");
|
||||
}
|
||||
|
||||
void apic_enable_timer()
|
||||
{
|
||||
/* set TPR to ensure interrupts are accepted */
|
||||
lapic_write(lapic_addr, LAPIC_TPR, 0);
|
||||
|
||||
/* set timer divisor */
|
||||
lapic_write(lapic_addr, LAPIC_TIMER_DIV, 0x3);
|
||||
|
||||
/* set timer initial count */
|
||||
lapic_write(lapic_addr, LAPIC_TIMER_INIT_COUNT, 0xFFF);
|
||||
|
||||
/* set timer vector and enable */
|
||||
lapic_write(lapic_addr, LAPIC_TIMER, 0x20 | 0x20000);
|
||||
}
|
||||
28
amd64/drivers/pio.S
Normal file
28
amd64/drivers/pio.S
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
.intel_syntax noprefix
|
||||
|
||||
.global outb
|
||||
outb:
|
||||
mov ax, si
|
||||
mov dx, di
|
||||
out dx, al
|
||||
ret
|
||||
|
||||
.global inb
|
||||
inb:
|
||||
mov dx, di
|
||||
in al, dx
|
||||
ret
|
||||
|
||||
.global outw
|
||||
outw:
|
||||
mov ax, si
|
||||
mov dx, di
|
||||
out dx, ax
|
||||
ret
|
||||
|
||||
.global inw
|
||||
inw:
|
||||
mov dx, di
|
||||
in ax, dx
|
||||
ret
|
||||
79
amd64/drivers/serial.c
Normal file
79
amd64/drivers/serial.c
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/device.h>
|
||||
|
||||
#include <drivers/serial.h>
|
||||
#include <drivers/pio.h>
|
||||
|
||||
#include <drivers/tty.h>
|
||||
|
||||
#include <lib/stdio.h>
|
||||
|
||||
struct serial_port {
|
||||
dev_t dev;
|
||||
int port;
|
||||
};
|
||||
|
||||
static int num_serial_ports = 0;
|
||||
static struct serial_port serial_ports[32];
|
||||
|
||||
static dev_t init_serial_port(int port)
|
||||
{
|
||||
outb(port | 1, 0x00);
|
||||
outb(port | 3, 0x80);
|
||||
outb(port | 0, 0x01);
|
||||
outb(port | 1, 0x00);
|
||||
outb(port | 3, 0x03);
|
||||
outb(port | 2, 0xC7);
|
||||
outb(port | 4, 0x0B);
|
||||
outb(port | 4, 0x1E);
|
||||
|
||||
outb(port | 0, 0xAE); /* Enable loopback mode */
|
||||
if (inb(port | 0) != 0xAE) { /* Loopback test */
|
||||
kwarn("Serial port 0x%04x loopback test failed\n", port);
|
||||
return 1;
|
||||
}
|
||||
|
||||
outb(port | 1, 0x01); /* Enable interrupts */
|
||||
outb(port | 4, 0x0F); /* Enter normal operation */
|
||||
|
||||
char name[256];
|
||||
|
||||
snprintf(name, sizeof(name) - 1, "tty%04x", port);
|
||||
name[sizeof(name) - 1] = 0;
|
||||
|
||||
dev_t serial_device = device_create(&serial_ports[num_serial_ports], DEV_CLASS_NATIVE_SERIAL, name);
|
||||
|
||||
if (num_serial_ports + 1 < ARRAY_SIZE(serial_ports)) {
|
||||
serial_ports[num_serial_ports].dev = serial_device;
|
||||
serial_ports[num_serial_ports].port = port;
|
||||
num_serial_ports += 1;
|
||||
}
|
||||
|
||||
return serial_device;
|
||||
}
|
||||
|
||||
dev_t arch_serial_init()
|
||||
{
|
||||
dev_t com1 = init_serial_port(COM1);
|
||||
return com1;
|
||||
}
|
||||
|
||||
void arch_serial_write(struct tty *tty, char c)
|
||||
{
|
||||
/* Wait for line to be clear */
|
||||
struct serial_port *serial_port = tty->serial_device_private;
|
||||
while ((inb(serial_port->port + 5) & 0x20) == 0)
|
||||
;
|
||||
outb(serial_port->port, c);
|
||||
}
|
||||
|
||||
char arch_serial_read(struct tty *tty)
|
||||
{
|
||||
struct serial_port *serial_port = tty->serial_device_private;
|
||||
|
||||
while ((inb(serial_port->port + 5) & 1) == 0)
|
||||
;
|
||||
|
||||
return inb(serial_port->port);
|
||||
}
|
||||
10
amd64/include/arch/msr.h
Normal file
10
amd64/include/arch/msr.h
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _MSR_H_
|
||||
#define _MSR_H_
|
||||
|
||||
#include <kernel/types.h>
|
||||
|
||||
u64 rdmsr(u32 msr);
|
||||
void wrmsr(u32 msr, u64 val);
|
||||
|
||||
#endif /* _MSR_H_ */
|
||||
32
amd64/include/arch/page.h
Normal file
32
amd64/include/arch/page.h
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _AMD64_PAGE_H_
|
||||
#define _AMD64_PAGE_H_
|
||||
|
||||
#include <kernel/page.h>
|
||||
|
||||
#define PAGE_SIZE 0x1000ull
|
||||
|
||||
#define PAGE_MASK_LOWER (~(PAGE_SIZE - 1))
|
||||
#define PAGE_MASK_UPPER ((PAGE_SIZE - 1))
|
||||
|
||||
#define PAGE_ALIGN (~(PAGE_SIZE - 1))
|
||||
|
||||
#define PAGE_PRESENT 0x001
|
||||
#define PAGE_RW 0x002
|
||||
#define PAGE_USER 0x004
|
||||
#define PAGE_WRITE_THRU 0x008
|
||||
#define PAGE_CACHE_DISABLE 0x010
|
||||
#define PAGE_ACCESSED 0x020
|
||||
#define PAGE_DIRTY 0x040
|
||||
#define PAGE_4MB_ENABLE 0x080
|
||||
|
||||
#define pgd_index(address) (((address) >> 39) & 0x1FF)
|
||||
#define pud_index(address) (((address) >> 30) & 0x1FF)
|
||||
#define pmd_index(address) (((address) >> 21) & 0x1FF)
|
||||
#define pte_index(address) (((address) >> 12) & 0x1FF)
|
||||
|
||||
void pgd_load(pgd_t *pgd);
|
||||
u64 *_pt_read(u64 *dir, size_t index, u64 *flags);
|
||||
void _pt_write(u64 *dir, size_t index, u64 *val, u64 flags);
|
||||
|
||||
#endif /* _AMD64_PAGE_H_ */
|
||||
44
amd64/include/drivers/apic.h
Normal file
44
amd64/include/drivers/apic.h
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _APIC_H_
|
||||
#define _APIC_H_
|
||||
|
||||
#define MADT_LAPIC 0
|
||||
#define MADT_IOAPIC 1
|
||||
#define MADT_IOAPIC_OVRD 2
|
||||
#define MADT_IOAPIC_NMI 3
|
||||
#define MADT_LAPIC_NMI 4
|
||||
#define MADT_LAPIC_OVRD 5
|
||||
#define MADT_X2_LAPIC 9
|
||||
|
||||
#define LAPIC_ID 0x20
|
||||
#define LAPIC_VERSION 0x30
|
||||
#define LAPIC_TPR 0x80
|
||||
#define LAPIC_APR 0x90
|
||||
#define LAPIC_PPR 0xA0
|
||||
#define LAPIC_EOI 0xB0
|
||||
#define LAPIC_RRD 0xC0
|
||||
#define LAPIC_LDR 0xD0
|
||||
#define LAPIC_DFR 0xE0
|
||||
#define LAPIC_SIVR 0xF0
|
||||
#define LAPIC_ISR 0x100
|
||||
#define LAPIC_TMR 0x180
|
||||
#define LAPIC_IRR 0x200
|
||||
#define LAPIC_ERR 0x280
|
||||
#define LAPIC_CMCI 0x2F0
|
||||
#define LAPIC_ICR 0x300
|
||||
#define LAPIC_THERMAL 0x330
|
||||
#define LAPIC_LVT_PERF_MON_COUNT 0x340
|
||||
#define LAPIC_LVT_LINT0 0x350
|
||||
#define LAPIC_LVT_LINT1 0x360
|
||||
#define LAPIC_LVT_ERR 0x370
|
||||
#define LAPIC_INIT_COUNT 0x380
|
||||
#define LAPIC_CUR_COUNT 0x390
|
||||
|
||||
#define LAPIC_TIMER 0x320
|
||||
#define LAPIC_TIMER_DIV 0x3E0
|
||||
#define LAPIC_TIMER_INIT_COUNT 0x380
|
||||
#define MSR_IA32_APIC_BASE 0x1B
|
||||
|
||||
void apic_init();
|
||||
|
||||
#endif /* _APIC_H_ */
|
||||
13
amd64/include/drivers/pio.h
Normal file
13
amd64/include/drivers/pio.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _PIO_H_
|
||||
#define _PIO_H_
|
||||
|
||||
#include <kernel/common.h>
|
||||
|
||||
void outb(u16 port, u8 byte);
|
||||
u8 inb(u16 port);
|
||||
|
||||
void outw(u16 port, u16 word);
|
||||
u16 inw(u16 port);
|
||||
|
||||
#endif /* _PIO_H_ */
|
||||
14
amd64/include/drivers/serial.h
Normal file
14
amd64/include/drivers/serial.h
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _SERIAL_H_
|
||||
#define _SERIAL_H_
|
||||
|
||||
#define COM1 0x3F8
|
||||
#define COM2 0x2F8
|
||||
|
||||
#define DEV_CLASS_NATIVE_SERIAL 0x9000
|
||||
|
||||
#include <kernel/device.h>
|
||||
|
||||
dev_t arch_serial_init();
|
||||
|
||||
#endif /* _SERIAL_H_ */
|
||||
12
amd64/include/kernel/spinlock.h
Normal file
12
amd64/include/kernel/spinlock.h
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#ifndef _SPINLOCK_H_
|
||||
#define _SPINLOCK_H_
|
||||
|
||||
#include <kernel/types.h>
|
||||
|
||||
typedef u64 spinlock_t;
|
||||
|
||||
void spinlock_acquire(spinlock_t *lock);
|
||||
void spinlock_release(spinlock_t *lock);
|
||||
|
||||
#endif /* _SPINLOCK_H_ */
|
||||
12
amd64/init/amd64_init.c
Normal file
12
amd64/init/amd64_init.c
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
void yield()
|
||||
{
|
||||
for (;;) {
|
||||
asm("hlt");
|
||||
}
|
||||
}
|
||||
|
||||
void _panic()
|
||||
{
|
||||
yield();
|
||||
}
|
||||
13
amd64/kernel/atomic.S
Normal file
13
amd64/kernel/atomic.S
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
.intel_syntax noprefix
|
||||
.global atomic_cmpxchg
|
||||
atomic_cmpxchg:
|
||||
mov eax, esi
|
||||
lock cmpxchg [rdi], edx
|
||||
|
||||
jnz nz
|
||||
xor eax, eax
|
||||
ret
|
||||
nz:
|
||||
mov eax, 1
|
||||
ret
|
||||
|
||||
21
amd64/kernel/spinlock.c
Normal file
21
amd64/kernel/spinlock.c
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/* SPDX-License-Identifier: GPL-2.0-only */
|
||||
#include <kernel/common.h>
|
||||
#include <kernel/spinlock.h>
|
||||
|
||||
int atomic_cmpxchg(spinlock_t *lock, int a, int b);
|
||||
|
||||
static uint64_t spinlock_attempt_acquire(spinlock_t *lock)
|
||||
{
|
||||
return atomic_cmpxchg(lock, 0, 1);
|
||||
}
|
||||
|
||||
void spinlock_acquire(spinlock_t *lock)
|
||||
{
|
||||
while (spinlock_attempt_acquire(lock))
|
||||
;
|
||||
}
|
||||
|
||||
void spinlock_release(spinlock_t *lock)
|
||||
{
|
||||
atomic_cmpxchg(lock, 1, 0);
|
||||
}
|
||||
Loading…
Reference in a new issue