minix/minix/kernel/arch/earm/pg_utils.c
google-labs-jules[bot] 581f21e31d Kernel Architecture Refactor (Foundational Pass)
This commit culminates a significant refactoring effort to address foundational architectural issues in the MINIX kernel, based on your initial technical review and subsequent detailed architectural roadmap feedback. It aims to establish a cleaner separation between kernel and userspace, introduce kernel-specific types and libraries, and prepare the build system for more robust kernel development.

**Key Achievements:**

1.  **Critical Fixes (Phase 0):**
    *   Corrected `kmemmove` implementation in `klib/kmemory.c` to prevent data corruption.
    *   Integrated i386 assembly files (`.S`) into the `meson.build` process, enabling kernel linkage.

2.  **Foundational Infrastructure (Phase 1 - Partial):**
    *   **Assertion Framework:**
        *   Introduced `include/sys/kassert.h` with `KASSERT` (conditional on `DEBUG_KERNEL`) and `KSTATIC_ASSERT` macros.
        *   Added `klib/kpanic.c` with a `kpanic` function (using `kprintf_stub` for now, relies on `extern arch_halt` and `arch_disable_interrupts`).
    *   **Memory Barriers:** Created `include/sys/barrier.h` with `kcompiler_barrier`, x86 memory barriers (`kmb`, `krmb`, `kwmb`), and SMP atomic operation barriers.
    *   **Enhanced Type System (`include/minix/kernel_types.h`):**
        *   Added `k_atomic_t`, `k_atomic_long_t`.
        *   Added opaque handle forward declarations (e.g., `k_proc_handle_t`).
        *   Added `K_CACHE_LINE_SIZE` and `__k_cacheline_aligned` macros.
        *   Added a kernel-safe `K_OFFSETOF` macro.
        *   Verified presence of previously added kernel-specific fixed-width integer types (e.g., `k_uint32_t`).
    *   **`offsetof` Replacement:** Replaced `offsetof` macro usage with `K_OFFSETOF` in files under `minix/kernel/` and `minix/kernel/system/`.

3.  **Initial Codebase Refactoring (Base for current work):**
    *   Established initial `kernel_types.h` with basic `k_` prefixed types.
    *   Created `klib` with `kstring.c`, `kmemory.c` (initial versions), and `kprintf_stub.c`.
    *   Systematically refactored all `.c` and `.h` files across `minix/kernel/` and its subdirectories (`system/`, `arch/i386/`, `arch/earm/`) to remove userspace headers, types, and function calls, replacing them with kernel-specific versions or `FIXME` placeholders.
    *   Overhauled `minix/kernel/meson.build` with kernel-specific flags, architecture handling, and definitions for `klib` and the kernel executable.

**Deferred Tasks & Known Next Steps:**

Due to environment limitations and the scale of changes, the following tasks are deferred for handling in a suitable build environment, ideally with scripting:
*   **`KASSERT_PLACEHOLDER` Migration:** Codebase-wide replacement of `KASSERT_PLACEHOLDER` with the new `KASSERT` macro and inclusion of `<sys/kassert.h>`.
*   **`offsetof` Migration (arch dirs):** Completion of `offsetof` to `K_OFFSETOF` replacement for `arch/i386/` and `arch/earm/` directories.

**Critical Outstanding Work (requiring a build environment):**
*   **Resolve FIXMEs:** Address all `FIXME` comments generated during refactoring. This includes defining numerous missing constants (error numbers, signal numbers, hardware constants), types (fixed-width if not covered, `struct exec_info`, `struct sigcontext`), and macros.
*   **Implement Missing `klib` Functions:** Provide full implementations for `kprintf`/`ksprintf` (currently stubs), `kstrncmp`, `kmemcmp`, `katoi`, `kstrlcat`, etc.
*   **Kernel Services:** Develop kernel-space signal handling, ELF loading mechanisms.
*   **Build System Enhancements:** Add full assembly language file support (specific flags, rules), manage linker scripts, and incorporate advanced linker options.
*   **HAL Implementation:** Define and implement Hardware Abstraction Layer interfaces as per the detailed roadmap.
*   **Thorough Build & Testing:** Iteratively build, resolve errors, and test on target hardware/emulators.

This commit represents a significant step towards a more robust MINIX kernel architecture. The subsequent phases outlined in your "Structured Implementation Roadmap" should be followed to achieve a production-ready state.
2025-06-07 00:07:54 +00:00

324 lines
8.0 KiB
C

#include <minix/cpufeature.h> // Kept for now
#include <minix/type.h> // Kept for now (appears twice, will be one)
// #include <assert.h> // Replaced
#include "kernel/kernel.h"
#include "arch_proto.h"
#include <machine/cpu.h> // Kept for now
#include <arm/armreg.h> // Kept for now
// #include <string.h> // Replaced
// Added kernel headers
#include <minix/kernel_types.h>
#include <klib/include/kprintf.h> // For KASSERT_PLACEHOLDER and kprintf_stub
#include <klib/include/kstring.h>
#include <klib/include/kmemory.h>
/* These are set/computed in kernel.lds. */
extern char _kern_vir_base, _kern_phys_base, _kern_size;
/* Retrieve the absolute values to something we can use. */
static phys_bytes kern_vir_start = (phys_bytes) &_kern_vir_base;
static phys_bytes kern_phys_start = (phys_bytes) &_kern_phys_base;
static phys_bytes kern_kernlen = (phys_bytes) &_kern_size;
/* page directory we can use to map things */
static u32_t pagedir[4096] __aligned(16384);
void print_memmap(kinfo_t *cbi)
{
int m;
KASSERT_PLACEHOLDER(cbi->mmap_size < MAXMEMMAP); // MODIFIED
for(m = 0; m < cbi->mmap_size; m++) {
phys_bytes addr = cbi->memmap[m].mm_base_addr, endit = cbi->memmap[m].mm_base_addr + cbi->memmap[m].mm_length;
kprintf_stub("%08lx-%08lx ",addr, endit); // MODIFIED
}
kprintf_stub("\nsize %08lx\n", cbi->mmap_size); // MODIFIED
}
void cut_memmap(kinfo_t *cbi, phys_bytes start, phys_bytes end)
{
int m;
phys_bytes o;
if((o=start % ARM_PAGE_SIZE))
start -= o;
if((o=end % ARM_PAGE_SIZE))
end += ARM_PAGE_SIZE - o;
KASSERT_PLACEHOLDER(kernel_may_alloc); // MODIFIED
for(m = 0; m < cbi->mmap_size; m++) {
phys_bytes substart = start, subend = end;
phys_bytes memaddr = cbi->memmap[m].mm_base_addr,
memend = cbi->memmap[m].mm_base_addr + cbi->memmap[m].mm_length;
/* adjust cut range to be a subset of the free memory */
if(substart < memaddr) substart = memaddr;
if(subend > memend) subend = memend;
if(substart >= subend) continue;
/* if there is any overlap, forget this one and add
* 1-2 subranges back
*/
cbi->memmap[m].mm_base_addr = cbi->memmap[m].mm_length = 0;
if(substart > memaddr)
add_memmap(cbi, memaddr, substart-memaddr);
if(subend < memend)
add_memmap(cbi, subend, memend-subend);
}
}
void add_memmap(kinfo_t *cbi, u64_t addr, u64_t len)
{
int m;
#define LIMIT 0xFFFFF000
/* Truncate available memory at 4GB as the rest of minix
* currently can't deal with any bigger.
*/
if(addr > LIMIT) {
return;
}
if(addr + len > LIMIT) {
len -= (addr + len - LIMIT);
}
KASSERT_PLACEHOLDER(cbi->mmap_size < MAXMEMMAP); // MODIFIED
if(len == 0) {
return;
}
addr = roundup(addr, ARM_PAGE_SIZE);
len = rounddown(len, ARM_PAGE_SIZE);
KASSERT_PLACEHOLDER(kernel_may_alloc); // MODIFIED
for(m = 0; m < MAXMEMMAP; m++) {
phys_bytes highmark;
if(cbi->memmap[m].mm_length) {
continue;
}
cbi->memmap[m].mm_base_addr = addr;
cbi->memmap[m].mm_length = len;
cbi->memmap[m].type = MULTIBOOT_MEMORY_AVAILABLE;
if(m >= cbi->mmap_size) {
cbi->mmap_size = m+1;
}
highmark = addr + len;
if(highmark > cbi->mem_high_phys) {
cbi->mem_high_phys = highmark;
}
return;
}
panic("no available memmap slot");
}
u32_t *alloc_pagetable(phys_bytes *ph)
{
u32_t *ret;
#define PG_PAGETABLES 24
static u32_t pagetables[PG_PAGETABLES][256] __aligned(1024);
static int pt_inuse = 0;
if(pt_inuse >= PG_PAGETABLES) {
panic("no more pagetables");
}
KASSERT_PLACEHOLDER(sizeof(pagetables[pt_inuse]) == 1024); // MODIFIED
ret = pagetables[pt_inuse++];
*ph = vir2phys(ret);
return ret;
}
#define PAGE_KB (ARM_PAGE_SIZE / 1024)
phys_bytes pg_alloc_page(kinfo_t *cbi)
{
int m;
multiboot_memory_map_t *mmap;
KASSERT_PLACEHOLDER(kernel_may_alloc); // MODIFIED
for(m = 0; m < cbi->mmap_size; m++) {
mmap = &cbi->memmap[m];
if(!mmap->mm_length) {
continue;
}
KASSERT_PLACEHOLDER(mmap->mm_length > 0); // MODIFIED
KASSERT_PLACEHOLDER(!(mmap->mm_length % ARM_PAGE_SIZE)); // MODIFIED
KASSERT_PLACEHOLDER(!(mmap->mm_base_addr % ARM_PAGE_SIZE)); // MODIFIED
u32_t addr = mmap->mm_base_addr;
mmap->mm_base_addr += ARM_PAGE_SIZE;
mmap->mm_length -= ARM_PAGE_SIZE;
cbi->kernel_allocated_bytes_dynamic += ARM_PAGE_SIZE;
return addr;
}
panic("can't find free memory");
}
void pg_identity(kinfo_t *cbi)
{
uint32_t i;
phys_bytes phys;
/* We map memory that does not correspond to physical memory
* as non-cacheable. Make sure we know what it is.
*/
KASSERT_PLACEHOLDER(cbi->mem_high_phys); // MODIFIED
/* Set up an identity mapping page directory */
for(i = 0; i < ARM_VM_DIR_ENTRIES; i++) {
u32_t flags = ARM_VM_SECTION
| ARM_VM_SECTION_USER
| ARM_VM_SECTION_DOMAIN;
phys = i * ARM_SECTION_SIZE;
/* mark mormal memory as cacheable. TODO: fix hard coded values */
if (phys >= PHYS_MEM_BEGIN && phys <= PHYS_MEM_END) {
pagedir[i] = phys | flags | ARM_VM_SECTION_CACHED;
} else {
pagedir[i] = phys | flags | ARM_VM_SECTION_DEVICE;
}
}
}
int pg_mapkernel(void)
{
int pde;
u32_t mapped = 0, kern_phys = kern_phys_start;
KASSERT_PLACEHOLDER(!(kern_vir_start % ARM_SECTION_SIZE)); // MODIFIED
KASSERT_PLACEHOLDER(!(kern_phys_start % ARM_SECTION_SIZE)); // MODIFIED
pde = kern_vir_start / ARM_SECTION_SIZE; /* start pde */
while(mapped < kern_kernlen) {
pagedir[pde] = (kern_phys & ARM_VM_SECTION_MASK)
| ARM_VM_SECTION
| ARM_VM_SECTION_SUPER
| ARM_VM_SECTION_DOMAIN
| ARM_VM_SECTION_CACHED;
mapped += ARM_SECTION_SIZE;
kern_phys += ARM_SECTION_SIZE;
pde++;
}
return pde; /* free pde */
}
void vm_enable_paging(void)
{
u32_t sctlr;
u32_t actlr;
write_ttbcr(0);
/* Set all Domains to Client */
write_dacr(0x55555555);
sctlr = read_sctlr();
/* Enable MMU */
sctlr |= CPU_CONTROL_MMU_ENABLE;
/* TRE set to zero (default reset value): TEX[2:0] are used, plus C and B bits.*/
sctlr &= ~CPU_CONTROL_TR_ENABLE;
/* AFE set to zero (default reset value): not using simplified model. */
sctlr &= ~CPU_CONTROL_AF_ENABLE;
/* Enable instruction ,data cache and branch prediction */
sctlr |= CPU_CONTROL_DC_ENABLE;
sctlr |= CPU_CONTROL_IC_ENABLE;
sctlr |= CPU_CONTROL_BPRD_ENABLE;
/* Enable barriers */
sctlr |= CPU_CONTROL_32BD_ENABLE;
/* Enable L2 cache (cortex-a8) */
#define CORTEX_A8_L2EN (0x02)
actlr = read_actlr();
actlr |= CORTEX_A8_L2EN;
write_actlr(actlr);
write_sctlr(sctlr);
}
phys_bytes pg_load(void)
{
phys_bytes phpagedir = vir2phys(pagedir);
write_ttbr0(phpagedir);
return phpagedir;
}
void pg_clear(void)
{
kmemset(pagedir, 0, sizeof(pagedir)); // MODIFIED
}
phys_bytes pg_rounddown(phys_bytes b)
{
phys_bytes o;
if(!(o = b % ARM_PAGE_SIZE)) {
return b;
}
return b - o;
}
void pg_map(phys_bytes phys, vir_bytes vaddr, vir_bytes vaddr_end,
kinfo_t *cbi)
{
static int mapped_pde = -1;
static u32_t *pt = NULL;
int pde, pte;
KASSERT_PLACEHOLDER(kernel_may_alloc); // MODIFIED
if(phys == PG_ALLOCATEME) {
KASSERT_PLACEHOLDER(!(vaddr % ARM_PAGE_SIZE)); // MODIFIED
} else {
KASSERT_PLACEHOLDER((vaddr % ARM_PAGE_SIZE) == (phys % ARM_PAGE_SIZE)); // MODIFIED
vaddr = pg_rounddown(vaddr);
phys = pg_rounddown(phys);
}
KASSERT_PLACEHOLDER(vaddr < kern_vir_start); // MODIFIED
while(vaddr < vaddr_end) {
phys_bytes source = phys;
KASSERT_PLACEHOLDER(!(vaddr % ARM_PAGE_SIZE)); // MODIFIED
if(phys == PG_ALLOCATEME) {
source = pg_alloc_page(cbi);
} else {
KASSERT_PLACEHOLDER(!(phys % ARM_PAGE_SIZE)); // MODIFIED
}
KASSERT_PLACEHOLDER(!(source % ARM_PAGE_SIZE)); // MODIFIED
pde = ARM_VM_PDE(vaddr);
pte = ARM_VM_PTE(vaddr);
if(mapped_pde < pde) {
phys_bytes ph;
pt = alloc_pagetable(&ph);
pagedir[pde] = (ph & ARM_VM_PDE_MASK)
| ARM_VM_PAGEDIR
| ARM_VM_PDE_DOMAIN;
mapped_pde = pde;
}
KASSERT_PLACEHOLDER(pt); // MODIFIED
pt[pte] = (source & ARM_VM_PTE_MASK)
| ARM_VM_PAGETABLE
| ARM_VM_PTE_CACHED
| ARM_VM_PTE_USER;
vaddr += ARM_PAGE_SIZE;
if(phys != PG_ALLOCATEME) {
phys += ARM_PAGE_SIZE;
}
}
}
void pg_info(reg_t *pagedir_ph, u32_t **pagedir_v)
{
*pagedir_ph = vir2phys(pagedir);
*pagedir_v = pagedir;
}