Changeset 35


Ignore:
Timestamp:
Jun 22, 2017, 8:10:37 AM (7 years ago)
Author:
max@…
Message:

Update:

  • Introduce a basic VM manager in hal_gpt.c, to create the initial page tree for cluster0.
  • Locate the ACPI RSDP table, which involves entering the PA into cluster0's VA space.

The definitions are still a bit messy, but they will be reorganized later.

Location:
trunk/hal/x86_64
Files:
3 added
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/hal/x86_64/hal_boot.h

    r29 r35  
    122122#define PDIR_SLOT_PTE   L4_SLOT_PTE
    123123
     124#define PTE_BASE        ((pt_entry_t *)(L4_SLOT_PTE * NBPD_L4))
     125#define L1_BASE PTE_BASE
     126#define L2_BASE ((pt_entry_t *)((char *)L1_BASE + L4_SLOT_PTE * NBPD_L3))
     127#define L3_BASE ((pt_entry_t *)((char *)L2_BASE + L4_SLOT_PTE * NBPD_L2))
     128#define L4_BASE ((pt_entry_t *)((char *)L3_BASE + L4_SLOT_PTE * NBPD_L1))
     129
     130#define NPDPG   (PAGE_SIZE / sizeof (pt_entry_t))
     131
     132#define CLUSTER0_MIN_VA 0xffff800000000000
     133#define CLUSTER0_MAX_VA 0xffff800100000000 /* MIN + 4GB */
     134
  • trunk/hal/x86_64/hal_cpu.S

    r29 r35  
    3535        ret
    3636
     37ASM_ENTRY(invlpg)
     38        invlpg  (%rdi)
     39        ret
     40
  • trunk/hal/x86_64/hal_gpt.c

    r25 r35  
    11/*
    2  * hal_gpt.c - implementation of the Generic Page Table API for TSAR-MIPS32
    3  *
    4  * Author   Alain Greiner (2016)
    5  *
    6  * Copyright (c) UPMC Sorbonne Universites
     2 * hal_gpt.c - implementation of the Generic Page Table API for x86_64
     3 *
     4 * Copyright (c) 2017 Maxime Villard
    75 *
    86 * This file is part of ALMOS-MKH.
    97 *
    10  * ALMOS-MKH.is free software; you can redistribute it and/or modify it
     8 * ALMOS-MKH is free software; you can redistribute it and/or modify it
    119 * under the terms of the GNU General Public License as published by
    1210 * the Free Software Foundation; version 2.0 of the License.
    1311 *
    14  * ALMOS-MKH.is distributed in the hope that it will be useful, but
     12 * ALMOS-MKH is distributed in the hope that it will be useful, but
    1513 * WITHOUT ANY WARRANTY; without even the implied warranty of
    1614 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     
    2321
    2422#include <hal_types.h>
     23#include <hal_boot.h> /* XXX */
    2524#include <hal_gpt.h>
    2625#include <hal_special.h>
     26#include <hal_internal.h>
     27
    2728#include <printk.h>
    2829#include <bits.h>
     30#include <string.h>
    2931#include <process.h>
    3032#include <kmem.h>
     
    3335#include <ppm.h>
    3436#include <page.h>
     37
     38#define VA_SIGN_MASK            0xffff000000000000
     39#define VA_SIGN_POS(va)         ((va) & ~VA_SIGN_MASK)
     40
     41#define pl1_i(VA)       (((VA_SIGN_POS(VA)) & L1_FRAME) >> L1_SHIFT)
     42#define pl2_i(VA)       (((VA_SIGN_POS(VA)) & L2_FRAME) >> L2_SHIFT)
     43#define pl3_i(VA)       (((VA_SIGN_POS(VA)) & L3_FRAME) >> L3_SHIFT)
     44#define pl4_i(VA)       (((VA_SIGN_POS(VA)) & L4_FRAME) >> L4_SHIFT)
     45
     46paddr_t pa_avail __in_kdata = 0;
     47vaddr_t va_avail __in_kdata = 0;
     48vaddr_t tmpva __in_kdata = (KERNBASE + NKL2_KIMG_ENTRIES * NBPD_L2);
     49
     50paddr_t hal_gpt_bootstrap_palloc(size_t npages)
     51{
     52        paddr_t pa = pa_avail;
     53        pa_avail += npages * PAGE_SIZE;
     54        return pa;
     55}
     56
     57vaddr_t hal_gpt_bootstrap_valloc(size_t npages)
     58{
     59        vaddr_t va = va_avail;
     60        va_avail += npages * PAGE_SIZE;
     61        return va;
     62}
     63
     64void hal_gpt_enter(vaddr_t va, paddr_t pa)
     65{
     66        PTE_BASE[pl1_i(va)] = (pa & PG_FRAME) | PG_V | PG_KW | PG_NX;
     67}
     68
     69/*
     70 * Create a page tree that can map va_start->va_end. The caller can then
     71 * enter these addresses to physical locations.
     72 *
     73 * This functions is a bit complicated, and may need to be revisited.
     74 */
     75void hal_gpt_maptree_area(vaddr_t va_start, vaddr_t va_end)
     76{
     77        size_t L4start, L4end, nL4e;
     78        size_t L3start, L3end, nL3e;
     79        size_t L2start, L2end, nL2e;
     80        paddr_t L3page, L2page, L1page;
     81        paddr_t pa;
     82        size_t i, npa;
     83        pt_entry_t *pde;
     84
     85        /* Allocate L3 */
     86        L4start = pl4_i(va_start);
     87        L4end = pl4_i(va_end);
     88        nL4e = (L4end - L4start + 1);
     89        L3page = hal_gpt_bootstrap_palloc(nL4e);
     90
     91        /* Allocate L2 */
     92        L3start = pl3_i(va_start);
     93        L3end = pl3_i(va_end);
     94        nL3e = (L3end - L3start + 1);
     95        L2page = hal_gpt_bootstrap_palloc(nL3e);
     96
     97        /* Allocate L1 */
     98        L2start = pl2_i(va_start);
     99        L2end = pl2_i(va_end);
     100        nL2e = (L2end - L2start + 1);
     101        L1page = hal_gpt_bootstrap_palloc(nL2e);
     102
     103        /* Zero out L1 */
     104        for (i = 0; i < nL2e; i++) {
     105                pa = L1page + i * PAGE_SIZE;
     106                hal_gpt_enter(tmpva, pa);
     107                invlpg(tmpva);
     108
     109                memset((void *)tmpva, 0, PAGE_SIZE);
     110        }
     111
     112        /* Zero out L2 */
     113        for (i = 0; i < nL3e; i++) {
     114                pa = L2page + i * PAGE_SIZE;
     115                hal_gpt_enter(tmpva, pa);
     116                invlpg(tmpva);
     117
     118                memset((void *)tmpva, 0, PAGE_SIZE);
     119        }
     120
     121        /* Zero out L3 */
     122        for (i = 0; i < nL4e; i++) {
     123                pa = L3page + i * PAGE_SIZE;
     124                hal_gpt_enter(tmpva, pa);
     125                invlpg(tmpva);
     126
     127                memset((void *)tmpva, 0, PAGE_SIZE);
     128        }
     129
     130        /* Create L2, linked to L1 */
     131        npa = (L2start / NPDPG) * PAGE_SIZE;
     132        for (i = L2start; i <= L2end; i++) {
     133                pa = (paddr_t)&(((pt_entry_t *)L2page)[i]);
     134                pa -= npa;      /* shift on the left */
     135                pa &= PG_FRAME; /* rounddown to a page boundary */
     136                hal_gpt_enter(tmpva, pa);
     137                invlpg(tmpva);
     138
     139                pde = (pt_entry_t *)tmpva;
     140                pa = L1page + (i - L2start) * PAGE_SIZE;
     141                pde[i % NPDPG] = (pa & PG_FRAME) | PG_V | PG_KW;
     142        }
     143
     144        /* Create L3, linked to L2 */
     145        npa = (L3start / NPDPG) * PAGE_SIZE;
     146        for (i = L3start; i <= L3end; i++) {
     147                pa = (paddr_t)&(((pt_entry_t *)L3page)[i]);
     148                pa -= npa;      /* shift on the left */
     149                pa &= PG_FRAME; /* rounddown to a page boundary */
     150                hal_gpt_enter(tmpva, pa);
     151                invlpg(tmpva);
     152
     153                pde = (pt_entry_t *)tmpva;
     154                pa = L2page + (i - L3start) * PAGE_SIZE;
     155                pde[i % NPDPG] = (pa & PG_FRAME) | PG_V | PG_KW;
     156        }
     157
     158        /* Link L3 into L4 */
     159        for (i = 0; i < nL4e; i++) {
     160                pa = L3page + i * PAGE_SIZE;
     161                L4_BASE[L4start + i] = (pa & PG_FRAME) | PG_V | PG_KW;
     162        }
     163}
     164
     165void hal_gpt_init(paddr_t firstpa)
     166{
     167        pa_avail = firstpa;
     168        va_avail = CLUSTER0_MIN_VA;
     169        hal_gpt_maptree_area(CLUSTER0_MIN_VA, CLUSTER0_MAX_VA);
     170}
     171
     172/* -------------------------------------------------------------------------- */
    35173
    36174/****************************************************************************************
  • trunk/hal/x86_64/hal_init.c

    r32 r35  
    2424#include <hal_multiboot.h>
    2525#include <hal_segmentation.h>
     26#include <hal_internal.h>
    2627
    2728#include <memcpy.h>
     
    3435#include <cluster.h>
    3536
    36 void x86_printf(char *s, ...);
    37 
    3837static void gdt_create();
    3938static void idt_create();
     
    4746#define offsetof(type, member) __builtin_offsetof(type, member)
    4847
    49 
    5048/* -------------------------------------------------------------------------- */
    5149
     
    9593
    9694        dump_memmap();
    97 
    9895        x86_printf("[+] dump finished\n");
     96
     97        hal_gpt_init(firstpa);
     98        x86_printf("[+] hal_gpt_init called\n");
     99
     100        hal_acpi_init();
     101        x86_printf("[+] hal_acpi_init called\n");
     102
     103
     104        int m = 0;
     105        int v = 1 / m;
    99106
    100107        char *buf = NULL;
    101108        *buf = (char)0x01;
    102 
    103         int m = 0;
    104         int v = 1 / m;
    105109
    106110        x86_printf("ALIVE!\n");
  • trunk/hal/x86_64/hal_types.h

    r34 r35  
    11/*
    2  * hal_types.h - common kernel types for MIPS 32 bits
     2 * hal_types.h - common kernel types for x86_64
    33 *
    44 * Author  Alain Greiner (2016)
Note: See TracChangeset for help on using the changeset viewer.