| 1 | /*
|
|---|
| 2 | * Copyright (C) 2001-2004 Jakub Jermar
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup xen32mm
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | * @ingroup xen32mm
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <mm/frame.h>
|
|---|
| 37 | #include <arch/mm/frame.h>
|
|---|
| 38 | #include <mm/as.h>
|
|---|
| 39 | #include <config.h>
|
|---|
| 40 | #include <arch/boot/boot.h>
|
|---|
| 41 | #include <arch/hypercall.h>
|
|---|
| 42 | #include <panic.h>
|
|---|
| 43 | #include <debug.h>
|
|---|
| 44 | #include <align.h>
|
|---|
| 45 | #include <macros.h>
|
|---|
| 46 |
|
|---|
| 47 | #include <console/cmd.h>
|
|---|
| 48 | #include <console/kconsole.h>
|
|---|
| 49 |
|
|---|
| 50 | uintptr_t last_frame = 0;
|
|---|
| 51 |
|
|---|
| 52 | #define L0_PT_SHIFT 10
|
|---|
| 53 | #define L3_PT_SHIFT 0
|
|---|
| 54 |
|
|---|
| 55 | #define L0_PT_ENTRIES 1024
|
|---|
| 56 | #define L3_PT_ENTRIES 1024
|
|---|
| 57 |
|
|---|
| 58 | #define L0_INDEX_MASK (L0_PT_ENTRIES - 1)
|
|---|
| 59 | #define L3_INDEX_MASK (L3_PT_ENTRIES - 1)
|
|---|
| 60 |
|
|---|
| 61 | #define PFN2PTL0_INDEX(pfn) ((pfn >> L0_PT_SHIFT) & L0_INDEX_MASK)
|
|---|
| 62 | #define PFN2PTL3_INDEX(pfn) ((pfn >> L3_PT_SHIFT) & L3_INDEX_MASK)
|
|---|
| 63 |
|
|---|
| 64 | #define PAGE_MASK (~(PAGE_SIZE - 1))
|
|---|
| 65 |
|
|---|
| 66 | #define _PAGE_PRESENT 0x001UL
|
|---|
| 67 | #define _PAGE_RW 0x002UL
|
|---|
| 68 | #define _PAGE_USER 0x004UL
|
|---|
| 69 | #define _PAGE_PWT 0x008UL
|
|---|
| 70 | #define _PAGE_PCD 0x010UL
|
|---|
| 71 | #define _PAGE_ACCESSED 0x020UL
|
|---|
| 72 | #define _PAGE_DIRTY 0x040UL
|
|---|
| 73 | #define _PAGE_PAT 0x080UL
|
|---|
| 74 | #define _PAGE_PSE 0x080UL
|
|---|
| 75 | #define _PAGE_GLOBAL 0x100UL
|
|---|
| 76 |
|
|---|
| 77 | #define L0_PROT (_PAGE_PRESENT | _PAGE_ACCESSED)
|
|---|
| 78 | #define L3_PROT (_PAGE_PRESENT | _PAGE_RW | _PAGE_ACCESSED)
|
|---|
| 79 |
|
|---|
| 80 | void frame_arch_init(void)
|
|---|
| 81 | {
|
|---|
| 82 | if (config.cpu_active == 1) {
|
|---|
| 83 | /* The only memory zone starts just after page table */
|
|---|
| 84 | pfn_t start = ADDR2PFN(ALIGN_UP(KA2PA(start_info.ptl0), PAGE_SIZE)) + start_info.pt_frames;
|
|---|
| 85 | size_t size = start_info.frames - start;
|
|---|
| 86 |
|
|---|
| 87 | /* Create identity mapping */
|
|---|
| 88 | pfn_t phys;
|
|---|
| 89 | count_t count = 0;
|
|---|
| 90 | for (phys = start; phys < start + size; phys++) {
|
|---|
| 91 | mmu_update_t updates[L3_PT_ENTRIES];
|
|---|
| 92 | pfn_t virt = ADDR2PFN(PA2KA(PFN2ADDR(phys)));
|
|---|
| 93 |
|
|---|
| 94 | size_t ptl0_index = PFN2PTL0_INDEX(virt);
|
|---|
| 95 | size_t ptl3_index = PFN2PTL3_INDEX(virt);
|
|---|
| 96 |
|
|---|
| 97 | pte_t *ptl3 = (pte_t *) PFN2ADDR(start_info.ptl0[ptl0_index].frame_address);
|
|---|
| 98 |
|
|---|
| 99 | if (ptl3 == 0) {
|
|---|
| 100 | mmuext_op_t mmu_ext;
|
|---|
| 101 |
|
|---|
| 102 | pfn_t virt2 = ADDR2PFN(PA2KA(PFN2ADDR(start)));
|
|---|
| 103 |
|
|---|
| 104 | /* New L1 page table entry needed */
|
|---|
| 105 | memsetb(PFN2ADDR(virt2), PAGE_SIZE, 0);
|
|---|
| 106 |
|
|---|
| 107 | size_t ptl0_index2 = PFN2PTL0_INDEX(virt2);
|
|---|
| 108 | size_t ptl3_index2 = PFN2PTL3_INDEX(virt2);
|
|---|
| 109 | pte_t *ptl3_2 = (pte_t *) PFN2ADDR(start_info.ptl0[ptl0_index2].frame_address);
|
|---|
| 110 |
|
|---|
| 111 | if (ptl3_2 == 0)
|
|---|
| 112 | panic("Unable to find page table reference");
|
|---|
| 113 |
|
|---|
| 114 | updates[count].ptr = (uintptr_t) &ptl3_2[ptl3_index2];
|
|---|
| 115 | updates[count].val = PA2MA(PFN2ADDR(start)) | L0_PROT;
|
|---|
| 116 | if (xen_mmu_update(updates, count + 1, NULL, DOMID_SELF) < 0)
|
|---|
| 117 | panic("Unable to map new page table");
|
|---|
| 118 | count = 0;
|
|---|
| 119 |
|
|---|
| 120 | mmu_ext.cmd = MMUEXT_PIN_L1_TABLE;
|
|---|
| 121 | mmu_ext.arg1.mfn = ADDR2PFN(PA2MA(PFN2ADDR(start)));
|
|---|
| 122 | if (xen_mmuext_op(&mmu_ext, 1, NULL, DOMID_SELF) < 0)
|
|---|
| 123 | panic("Error pinning new page table");
|
|---|
| 124 |
|
|---|
| 125 | pte_t *ptl0 = (pte_t *) PA2MA(KA2PA(start_info.ptl0));
|
|---|
| 126 |
|
|---|
| 127 | updates[count].ptr = (uintptr_t) &ptl0[ptl0_index];
|
|---|
| 128 | updates[count].val = PA2MA(PFN2ADDR(start)) | L3_PROT;
|
|---|
| 129 | if (xen_mmu_update(updates, count + 1, NULL, DOMID_SELF) < 0)
|
|---|
| 130 | panic("Unable to update PTE for page table");
|
|---|
| 131 | count = 0;
|
|---|
| 132 |
|
|---|
| 133 | ptl3 = (pte_t *) PFN2ADDR(start_info.ptl0[ptl0_index].frame_address);
|
|---|
| 134 | start++;
|
|---|
| 135 | size--;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | updates[count].ptr = (uintptr_t) &ptl3[ptl3_index];
|
|---|
| 139 | updates[count].val = PA2MA(PFN2ADDR(phys)) | L3_PROT;
|
|---|
| 140 | count++;
|
|---|
| 141 |
|
|---|
| 142 | if ((count == L3_PT_ENTRIES) || (phys + 1 == start + size)) {
|
|---|
| 143 | if (xen_mmu_update(updates, count, NULL, DOMID_SELF) < 0)
|
|---|
| 144 | panic("Unable to update PTE");
|
|---|
| 145 | count = 0;
|
|---|
| 146 | }
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | zone_create(start, size, start, 0);
|
|---|
| 150 | last_frame = start + size;
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /** @}
|
|---|
| 155 | */
|
|---|