Merge branch 'master' of ssh://review.rcs.uwaterloo.ca:77/source/metal-cos

This commit is contained in:
Ali Mashtizadeh 2023-10-23 22:00:07 -04:00
commit cf1adfc2a4
28 changed files with 92 additions and 56 deletions

View File

@ -54,7 +54,7 @@ env.Append(CFLAGS = [ "-Wshadow", "-Wno-typedef-redefinition" ])
env.Append(CPPFLAGS = [ "-target", "x86_64-freebsd-freebsd-elf", env.Append(CPPFLAGS = [ "-target", "x86_64-freebsd-freebsd-elf",
"-fno-builtin", "-fno-stack-protector", "-fno-builtin", "-fno-stack-protector",
"-fno-optimize-sibling-calls" ]) "-fno-optimize-sibling-calls" ])
env.Append(LINKFLAGS = [ "-no-pie" ]) #env.Append(LINKFLAGS = [ "-no-pie" ])
if (env["STRICT"] == "1"): if (env["STRICT"] == "1"):
env.Append(CPPFLAGS = [ "-Wformat=2", "-Wmissing-format-attribute", env.Append(CPPFLAGS = [ "-Wformat=2", "-Wmissing-format-attribute",

View File

@ -3,12 +3,8 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/mman.h> #include <sys/mman.h>
#if defined(__x86_64__)
#include <sys/cdefs.h> #include <sys/cdefs.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#else
#error "Unsupported Architecture!"
#endif
#include <syscall.h> #include <syscall.h>

View File

@ -7,8 +7,8 @@
#include <sys/kdebug.h> #include <sys/kdebug.h>
#include <sys/mp.h> #include <sys/mp.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/amd64op.h> #include <machine/cpuop.h>
#include <machine/atomic.h> #include <machine/atomic.h>
#include <machine/trap.h> #include <machine/trap.h>
#include <machine/lapic.h> #include <machine/lapic.h>

View File

@ -12,7 +12,7 @@
* Page Tables * Page Tables
*/ */
#define PGNUMMASK 0xFFFFFFFFFFFFF000ULL #define PGNUMMASK 0x7FFFFFFFFFFFF000ULL
#define PGIDXSHIFT 9 #define PGIDXSHIFT 9
#define PGIDXMASK (512 - 1) #define PGIDXMASK (512 - 1)
@ -223,7 +223,7 @@ typedef struct XSAVEArea
#define MSR_CSTAR 0xC0000083 #define MSR_CSTAR 0xC0000083
#define MSR_SFMASK 0xC0000084 #define MSR_SFMASK 0xC0000084
#include "amd64op.h" #include "cpuop.h"
#endif /* __AMD64_H__ */ #endif /* __AMD64_H__ */

View File

@ -2,7 +2,7 @@
#ifndef __PMAP_H__ #ifndef __PMAP_H__
#define __PMAP_H__ #define __PMAP_H__
#include <machine/amd64.h> #include <machine/cpu.h>
/* /*
* +----------------------+ * +----------------------+

View File

@ -2,8 +2,8 @@
#ifndef __MACHINE_THREAD_H__ #ifndef __MACHINE_THREAD_H__
#define __MACHINE_THREAD_H__ #define __MACHINE_THREAD_H__
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/amd64op.h> #include <machine/cpuop.h>
typedef struct ThreadArchStackFrame { typedef struct ThreadArchStackFrame {
uint64_t r15; uint64_t r15;

View File

@ -9,7 +9,7 @@
#include <sys/kdebug.h> #include <sys/kdebug.h>
#include <sys/cdefs.h> #include <sys/cdefs.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/pmap.h> #include <machine/pmap.h>
#include <machine/trap.h> #include <machine/trap.h>

View File

@ -8,8 +8,8 @@
#include <sys/kassert.h> #include <sys/kassert.h>
#include <sys/kdebug.h> #include <sys/kdebug.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/amd64op.h> #include <machine/cpuop.h>
#include <machine/pmap.h> #include <machine/pmap.h>
#include <machine/trap.h> #include <machine/trap.h>

View File

@ -8,7 +8,7 @@
#include <sys/irq.h> #include <sys/irq.h>
#include <sys/spinlock.h> #include <sys/spinlock.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/ioapic.h> #include <machine/ioapic.h>
#include <machine/lapic.h> #include <machine/lapic.h>
#include <machine/trap.h> #include <machine/trap.h>

View File

@ -10,7 +10,7 @@
#include "../dev/console.h" #include "../dev/console.h"
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/pmap.h> #include <machine/pmap.h>
#include <machine/multiboot.h> #include <machine/multiboot.h>

View File

@ -10,8 +10,8 @@
#include <sys/ktime.h> #include <sys/ktime.h>
#include <sys/mp.h> #include <sys/mp.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/amd64op.h> #include <machine/cpuop.h>
#include <machine/pmap.h> #include <machine/pmap.h>
#include <machine/lapic.h> #include <machine/lapic.h>
#include <machine/mp.h> #include <machine/mp.h>

View File

@ -4,8 +4,8 @@
#include <sys/kassert.h> #include <sys/kassert.h>
#include <sys/pci.h> #include <sys/pci.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/amd64op.h> #include <machine/cpuop.h>
#define PCI_PORT_ADDR 0xCF8 #define PCI_PORT_ADDR 0xCF8
#define PCI_PORT_DATABASE 0xCFC #define PCI_PORT_DATABASE 0xCFC

View File

@ -6,15 +6,40 @@
#include <sys/kassert.h> #include <sys/kassert.h>
#include <sys/kdebug.h> #include <sys/kdebug.h>
#include <sys/kmem.h> #include <sys/kmem.h>
#include <sys/mman.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/amd64op.h> #include <machine/cpuop.h>
#include <machine/mp.h> #include <machine/mp.h>
#include <machine/pmap.h> #include <machine/pmap.h>
AS systemAS; AS systemAS;
AS *currentAS[MAX_CPUS]; AS *currentAS[MAX_CPUS];
static uint64_t
PMapProtToPTE(uint64_t flags)
{
uint64_t pte = 0;
ASSERT(flags != 0);
if (flags & PROT_READ)
pte |= PTE_P;
if (flags & PROT_WRITE)
pte |= PTE_P|PTE_W;
if (flags & PROT_EXEC)
pte |= PTE_P;
else
pte |= PTE_NX;
ASSERT(pte != 0);
if (flags & MAP_NOCACHE)
pte |= PTE_PCD;
return pte;
}
void void
PMap_Init() PMap_Init()
{ {
@ -52,11 +77,11 @@ PMap_Init()
// Setup system mappings // Setup system mappings
PMap_SystemLMap(0x0, MEM_DIRECTMAP_BASE + 0x0, PMap_SystemLMap(0x0, MEM_DIRECTMAP_BASE + 0x0,
3*512, 0); // 3GB RWX 3*512, PROT_ALL); // 3GB RWX
PMap_SystemLMap(0xC0000000, MEM_DIRECTMAP_BASE + 0xC0000000, PMap_SystemLMap(0xC0000000, MEM_DIRECTMAP_BASE + 0xC0000000,
512, PTE_NX|PTE_PCD); // 1GB RW + PCD 512, PROT_ALL|MAP_NOCACHE); // 1GB RW + PCD
PMap_SystemLMap(0x100000000, MEM_DIRECTMAP_BASE + 0x100000000, PMap_SystemLMap(0x100000000, MEM_DIRECTMAP_BASE + 0x100000000,
60*512, 0); // 60GB RWX 60*512, PROT_ALL); // 60GB RWX
PMap_LoadAS(&systemAS); PMap_LoadAS(&systemAS);
@ -248,7 +273,7 @@ PMap_Translate(AS *space, uintptr_t va)
if ((pte & PTE_PS) == PTE_PS) { if ((pte & PTE_PS) == PTE_PS) {
// Handle 2MB pages // Handle 2MB pages
entry = &table->entries[k]; entry = &table->entries[k];
return (*entry & ~(LARGE_PGMASK | PTE_NX)) + (va & LARGE_PGMASK); return (*entry & PGNUMMASK) + (va & LARGE_PGMASK);
} }
if (pte == 0) { if (pte == 0) {
ASSERT(pte); ASSERT(pte);
@ -259,7 +284,7 @@ PMap_Translate(AS *space, uintptr_t va)
// Handle 4KB pages // Handle 4KB pages
entry = &table->entries[l]; entry = &table->entries[l];
return (*entry & ~(PGMASK | PTE_NX)) + (va & PGMASK); return (*entry & PGNUMMASK) + (va & PGMASK);
} }
/** /**
@ -356,6 +381,7 @@ PMap_Map(AS *as, uint64_t phys, uint64_t virt, uint64_t pages, uint64_t flags)
{ {
int i; int i;
PageEntry *entry; PageEntry *entry;
uint64_t pteflags = PMapProtToPTE(flags);
for (i = 0; i < pages; i++) { for (i = 0; i < pages; i++) {
uint64_t va = virt + PGSIZE * i; uint64_t va = virt + PGSIZE * i;
@ -365,7 +391,7 @@ PMap_Map(AS *as, uint64_t phys, uint64_t virt, uint64_t pages, uint64_t flags)
return false; return false;
} }
*entry = (phys + PGSIZE * i) | PTE_P | PTE_W | PTE_U | flags; *entry = (phys + PGSIZE * i) | PTE_U | pteflags;
} }
return true; return true;
@ -425,6 +451,7 @@ PMap_AllocMap(AS *as, uint64_t virt, uint64_t len, uint64_t flags)
int i; int i;
uint64_t pages = (len + PGSIZE - 1) / PGSIZE; uint64_t pages = (len + PGSIZE - 1) / PGSIZE;
PageEntry *entry; PageEntry *entry;
uint64_t pteflags = PMapProtToPTE(flags);
ASSERT((virt & PGMASK) == 0); ASSERT((virt & PGMASK) == 0);
@ -438,7 +465,7 @@ PMap_AllocMap(AS *as, uint64_t virt, uint64_t len, uint64_t flags)
if ((*entry & PTE_P) != PTE_P) { if ((*entry & PTE_P) != PTE_P) {
void *pg = PAlloc_AllocPage(); void *pg = PAlloc_AllocPage();
*entry = (uint64_t)DMVA2PA(pg) | PTE_P | PTE_U | flags; *entry = (uint64_t)DMVA2PA(pg) | PTE_U | pteflags;
} }
} }
@ -481,6 +508,7 @@ PMap_SystemLMap(uint64_t phys, uint64_t virt, uint64_t lpages, uint64_t flags)
{ {
int i; int i;
PageEntry *entry; PageEntry *entry;
uint64_t pteflags = PMapProtToPTE(flags);
for (i = 0; i < lpages; i++) { for (i = 0; i < lpages; i++) {
uint64_t va = virt + LARGE_PGSIZE * i; uint64_t va = virt + LARGE_PGSIZE * i;
@ -490,7 +518,7 @@ PMap_SystemLMap(uint64_t phys, uint64_t virt, uint64_t lpages, uint64_t flags)
return false; return false;
} }
*entry = (phys + LARGE_PGSIZE * i) | PTE_P | PTE_W | PTE_PS | flags; *entry = (phys + LARGE_PGSIZE * i) | PTE_PS | pteflags;
} }
return true; return true;
@ -515,6 +543,7 @@ PMap_SystemMap(uint64_t phys, uint64_t virt, uint64_t pages, uint64_t flags)
{ {
int i; int i;
PageEntry *entry; PageEntry *entry;
uint64_t pteflags = PMapProtToPTE(flags);
for (i = 0; i < pages; i++) { for (i = 0; i < pages; i++) {
uint64_t va = virt + PGSIZE * i; uint64_t va = virt + PGSIZE * i;
@ -524,7 +553,7 @@ PMap_SystemMap(uint64_t phys, uint64_t virt, uint64_t pages, uint64_t flags)
return false; return false;
} }
*entry = (phys + PGSIZE * i) | PTE_P | PTE_W | flags; *entry = (phys + PGSIZE * i) | pteflags;
} }
return true; return true;

View File

@ -8,8 +8,8 @@
#include <sys/kmem.h> #include <sys/kmem.h>
#include <sys/mp.h> #include <sys/mp.h>
#include <sys/thread.h> #include <sys/thread.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/amd64op.h> #include <machine/cpuop.h>
#include <machine/trap.h> #include <machine/trap.h>
#include <machine/pmap.h> #include <machine/pmap.h>

View File

@ -6,8 +6,8 @@
#include <sys/kdebug.h> #include <sys/kdebug.h>
#include <sys/ktime.h> #include <sys/ktime.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/amd64op.h> #include <machine/cpuop.h>
uint64_t uint64_t
Time_GetTSC() Time_GetTSC()

View File

@ -12,7 +12,7 @@
#include <sys/syscall.h> #include <sys/syscall.h>
#include <sys/mp.h> #include <sys/mp.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/lapic.h> #include <machine/lapic.h>
#include <machine/trap.h> #include <machine/trap.h>
#include <machine/mp.h> #include <machine/mp.h>

View File

@ -7,8 +7,8 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/amd64op.h> #include <machine/cpuop.h>
void DebugConsole_Init() void DebugConsole_Init()
{ {

View File

@ -7,8 +7,8 @@
#include <sys/kdebug.h> #include <sys/kdebug.h>
#include <sys/irq.h> #include <sys/irq.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/amd64op.h> #include <machine/cpuop.h>
#include "../console.h" #include "../console.h"
#include "ps2.h" #include "ps2.h"

View File

@ -8,7 +8,7 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#include <sys/kdebug.h> #include <sys/kdebug.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/pmap.h> #include <machine/pmap.h>
#include "vgacons.h" #include "vgacons.h"

View File

@ -6,11 +6,16 @@
#define PROT_READ 0x01 #define PROT_READ 0x01
#define PROT_WRITE 0x02 #define PROT_WRITE 0x02
#define PROT_EXEC 0x04 #define PROT_EXEC 0x04
#define PROT_ALL (PROT_READ|PROT_WRITE|PROT_EXEC)
#define MAP_FILE 0x0010 #define MAP_FILE 0x0010
#define MAP_ANON 0x0020 #define MAP_ANON 0x0020
#define MAP_FIXED 0x0040 #define MAP_FIXED 0x0040
#ifdef _KERNEL
#define MAP_NOCACHE 0x1000
#endif
#ifdef _KERNEL #ifdef _KERNEL
#else /* _KERNEL */ #else /* _KERNEL */

View File

@ -8,8 +8,8 @@
#include <sys/mp.h> #include <sys/mp.h>
#include <sys/spinlock.h> #include <sys/spinlock.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/amd64op.h> #include <machine/cpuop.h>
uint32_t lockLevel[MAX_CPUS]; uint32_t lockLevel[MAX_CPUS];

View File

@ -13,8 +13,8 @@
#include <sys/queue.h> #include <sys/queue.h>
#include <sys/disk.h> #include <sys/disk.h>
#include <sys/elf64.h> #include <sys/elf64.h>
#include <sys/mman.h>
#include <machine/amd64.h>
#include <machine/trap.h> #include <machine/trap.h>
#include <machine/pmap.h> #include <machine/pmap.h>
#include <sys/thread.h> #include <sys/thread.h>
@ -158,7 +158,7 @@ Loader_Load(Thread *thr, VNode *vn, void *buf, uint64_t len)
memsz += phdr[i].p_vaddr - va; memsz += phdr[i].p_vaddr - va;
Log(loader, "AllocMap %016llx %08llx\n", va, memsz); Log(loader, "AllocMap %016llx %08llx\n", va, memsz);
if (!PMap_AllocMap(as, va, memsz, PTE_W)) { if (!PMap_AllocMap(as, va, memsz, PROT_ALL)) {
// XXX: Cleanup! // XXX: Cleanup!
ASSERT(false); ASSERT(false);
return false; return false;
@ -166,7 +166,7 @@ Loader_Load(Thread *thr, VNode *vn, void *buf, uint64_t len)
} }
} }
PMap_AllocMap(as, MEM_USERSPACE_STKBASE, MEM_USERSPACE_STKLEN, PTE_W); PMap_AllocMap(as, MEM_USERSPACE_STKBASE, MEM_USERSPACE_STKLEN, PROT_READ|PROT_WRITE);
for (i = 0; i < ehdr->e_phnum; i++) for (i = 0; i < ehdr->e_phnum; i++)
{ {
@ -238,6 +238,7 @@ Loader_LoadInit()
*/ */
PMap_LoadAS(thr->space); // Reload CR3 PMap_LoadAS(thr->space); // Reload CR3
#if defined(__x86_64__)
/* /*
* Pass in zero arguments with null pointers to init * Pass in zero arguments with null pointers to init
*/ */
@ -263,6 +264,9 @@ Loader_LoadInit()
tf.rflags = RFLAGS_IF; tf.rflags = RFLAGS_IF;
tf.rdi = rsp; tf.rdi = rsp;
Trap_Pop(&tf); Trap_Pop(&tf);
#elif defined(__aarch64__)
NOT_IMPLEMENTED();
#endif
/* /*
* We should never reach this point! * We should never reach this point!

View File

@ -16,7 +16,7 @@
#include <sys/spinlock.h> #include <sys/spinlock.h>
// PGSIZE // PGSIZE
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/pmap.h> #include <machine/pmap.h>
/* 'FREEPAGE' */ /* 'FREEPAGE' */

View File

@ -15,8 +15,8 @@
#include <sys/spinlock.h> #include <sys/spinlock.h>
#include <machine/atomic.h> #include <machine/atomic.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/amd64op.h> #include <machine/cpuop.h>
Spinlock lockListLock = { Spinlock lockListLock = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,

View File

@ -228,7 +228,7 @@ Syscall_MMap(uint64_t addr, uint64_t len, uint64_t prot)
Thread *cur = Sched_Current(); Thread *cur = Sched_Current();
bool status; bool status;
status = PMap_AllocMap(cur->space, addr, len, PTE_W); status = PMap_AllocMap(cur->space, addr, len, prot);
Thread_Release(cur); Thread_Release(cur);
if (!status) { if (!status) {
// XXX: Need to unmap PMap_Unmap(cur->space, addr, pgs); // XXX: Need to unmap PMap_Unmap(cur->space, addr, pgs);

View File

@ -15,6 +15,7 @@
#include <sys/kdebug.h> #include <sys/kdebug.h>
#include <sys/kmem.h> #include <sys/kmem.h>
#include <sys/ktime.h> #include <sys/ktime.h>
#include <sys/mman.h>
#include <sys/mp.h> #include <sys/mp.h>
#include <sys/spinlock.h> #include <sys/spinlock.h>
#include <sys/thread.h> #include <sys/thread.h>
@ -171,7 +172,7 @@ Thread_UThreadCreate(Thread *oldThr, uint64_t rip, uint64_t arg)
proc->ustackNext += MEM_USERSPACE_STKLEN; proc->ustackNext += MEM_USERSPACE_STKLEN;
Spinlock_Unlock(&proc->lock); Spinlock_Unlock(&proc->lock);
PMap_AllocMap(thr->space, thr->ustack, MEM_USERSPACE_STKLEN, PTE_W); PMap_AllocMap(thr->space, thr->ustack, MEM_USERSPACE_STKLEN, PROT_READ|PROT_WRITE);
// XXX: Check failure // XXX: Check failure
Thread_InitArch(thr); Thread_InitArch(thr);

View File

@ -6,9 +6,10 @@
#include <sys/kassert.h> #include <sys/kassert.h>
#include <sys/kdebug.h> #include <sys/kdebug.h>
#include <sys/kmem.h> #include <sys/kmem.h>
#include <sys/mman.h>
#include <machine/amd64.h> #include <machine/cpu.h>
#include <machine/amd64op.h> #include <machine/cpuop.h>
#include <machine/pmap.h> #include <machine/pmap.h>
#include <machine/mp.h> #include <machine/mp.h>
@ -106,7 +107,7 @@ XMem_Allocate(XMem *xmem, uintptr_t length)
if (pg == NULL) if (pg == NULL)
return false; return false;
PMap_SystemMap(DMVA2PA((uint64_t)pg), xmem->base + off, 1, 0); PMap_SystemMap(DMVA2PA((uint64_t)pg), xmem->base + off, 1, PROT_ALL);
xmem->length += PGSIZE; xmem->length += PGSIZE;
} }