[0356274] | 1 | /*
|
---|
[e2882a7] | 2 | * Copyright (C) 2006 Martin Decky
|
---|
[0356274] | 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 |
|
---|
[57ce359] | 29 | /** @addtogroup ia32xen
|
---|
[0356274] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
| 35 | #include <arch.h>
|
---|
[deaa22f] | 36 | #include <main/main.h>
|
---|
[0356274] | 37 |
|
---|
| 38 | #include <arch/types.h>
|
---|
| 39 | #include <typedefs.h>
|
---|
[e12ccc5] | 40 | #include <align.h>
|
---|
[0356274] | 41 |
|
---|
| 42 | #include <arch/pm.h>
|
---|
| 43 |
|
---|
[aecf79f] | 44 | #include <arch/drivers/xconsole.h>
|
---|
[e12ccc5] | 45 | #include <arch/mm/page.h>
|
---|
[0356274] | 46 |
|
---|
| 47 | #include <arch/context.h>
|
---|
| 48 |
|
---|
| 49 | #include <config.h>
|
---|
| 50 |
|
---|
| 51 | #include <arch/interrupt.h>
|
---|
| 52 | #include <arch/asm.h>
|
---|
| 53 | #include <genarch/acpi/acpi.h>
|
---|
| 54 |
|
---|
| 55 | #include <arch/bios/bios.h>
|
---|
| 56 |
|
---|
[4965a846] | 57 | #include <arch/boot/boot.h>
|
---|
[0356274] | 58 | #include <arch/mm/memory_init.h>
|
---|
| 59 | #include <interrupt.h>
|
---|
| 60 | #include <arch/debugger.h>
|
---|
| 61 | #include <proc/thread.h>
|
---|
| 62 | #include <syscall/syscall.h>
|
---|
| 63 | #include <console/console.h>
|
---|
[ec04b20] | 64 | #include <ddi/irq.h>
|
---|
[0356274] | 65 |
|
---|
[4965a846] | 66 | start_info_t start_info;
|
---|
[e12ccc5] | 67 | memzone_t meminfo;
|
---|
| 68 |
|
---|
[7d3d641] | 69 | extern void xen_callback(void);
|
---|
| 70 | extern void xen_failsafe_callback(void);
|
---|
| 71 |
|
---|
[e12ccc5] | 72 | void arch_pre_main(void)
|
---|
| 73 | {
|
---|
| 74 | xen_vm_assist(VMASST_CMD_ENABLE, VMASST_TYPE_WRITABLE_PAGETABLES);
|
---|
| 75 |
|
---|
| 76 | pte_t pte;
|
---|
| 77 | memsetb((uintptr_t) &pte, sizeof(pte), 0);
|
---|
| 78 |
|
---|
| 79 | pte.present = 1;
|
---|
| 80 | pte.writeable = 1;
|
---|
| 81 | pte.frame_address = ADDR2PFN((uintptr_t) start_info.shared_info);
|
---|
| 82 | xen_update_va_mapping(&shared_info, pte, UVMF_INVLPG);
|
---|
| 83 |
|
---|
[adf7f9c] | 84 | pte.present = 1;
|
---|
| 85 | pte.writeable = 1;
|
---|
| 86 | pte.frame_address = start_info.console_mfn;
|
---|
| 87 | xen_update_va_mapping(&console_page, pte, UVMF_INVLPG);
|
---|
| 88 |
|
---|
[7d3d641] | 89 | xen_set_callbacks(XEN_CS, xen_callback, XEN_CS, xen_failsafe_callback);
|
---|
| 90 |
|
---|
[e12ccc5] | 91 | /* Create identity mapping */
|
---|
| 92 |
|
---|
| 93 | meminfo.start = ADDR2PFN(ALIGN_UP(KA2PA(start_info.ptl0), PAGE_SIZE)) + start_info.pt_frames;
|
---|
| 94 | meminfo.size = start_info.frames - meminfo.start;
|
---|
| 95 | meminfo.reserved = 0;
|
---|
| 96 |
|
---|
| 97 | uintptr_t pa;
|
---|
| 98 | index_t last_ptl0 = 0;
|
---|
| 99 | for (pa = PFN2ADDR(meminfo.start); pa < PFN2ADDR(meminfo.start + meminfo.size); pa += FRAME_SIZE) {
|
---|
| 100 | uintptr_t va = PA2KA(pa);
|
---|
| 101 |
|
---|
| 102 | if ((PTL0_INDEX(va) != last_ptl0) && (GET_PTL1_FLAGS(start_info.ptl0, PTL0_INDEX(va)) & PAGE_NOT_PRESENT)) {
|
---|
| 103 | /* New page directory entry needed */
|
---|
| 104 | uintptr_t tpa = PFN2ADDR(meminfo.start + meminfo.reserved);
|
---|
| 105 | uintptr_t tva = PA2KA(tpa);
|
---|
| 106 |
|
---|
| 107 | memsetb(tva, PAGE_SIZE, 0);
|
---|
| 108 |
|
---|
| 109 | pte_t *tptl3 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(start_info.ptl0, PTL0_INDEX(tva)));
|
---|
| 110 | SET_FRAME_FLAGS(tptl3, PTL3_INDEX(tva), PAGE_PRESENT);
|
---|
| 111 | SET_PTL1_ADDRESS(start_info.ptl0, PTL0_INDEX(va), tpa);
|
---|
| 112 |
|
---|
| 113 | last_ptl0 = PTL0_INDEX(va);
|
---|
| 114 | meminfo.reserved++;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | pte_t *ptl3 = (pte_t *) PA2KA(GET_PTL1_ADDRESS(start_info.ptl0, PTL0_INDEX(va)));
|
---|
| 118 |
|
---|
| 119 | SET_FRAME_ADDRESS(ptl3, PTL3_INDEX(va), pa);
|
---|
| 120 | SET_FRAME_FLAGS(ptl3, PTL3_INDEX(va), PAGE_PRESENT | PAGE_WRITE);
|
---|
| 121 | }
|
---|
[bf78569] | 122 |
|
---|
| 123 | /* Put initial stack safely in the mapped area */
|
---|
| 124 | stack_safe = PA2KA(PFN2ADDR(meminfo.start + meminfo.reserved));
|
---|
[e12ccc5] | 125 | }
|
---|
[4965a846] | 126 |
|
---|
[0356274] | 127 | void arch_pre_mm_init(void)
|
---|
| 128 | {
|
---|
[aecf79f] | 129 | pm_init();
|
---|
[0356274] | 130 |
|
---|
| 131 | if (config.cpu_active == 1) {
|
---|
[ec04b20] | 132 | interrupt_init();
|
---|
[0356274] | 133 | // bios_init();
|
---|
| 134 |
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | void arch_post_mm_init(void)
|
---|
| 139 | {
|
---|
| 140 | if (config.cpu_active == 1) {
|
---|
[ec04b20] | 141 | /* Initialize IRQ routing */
|
---|
| 142 | irq_init(IRQ_COUNT, IRQ_COUNT);
|
---|
| 143 |
|
---|
| 144 | /* Video */
|
---|
[aecf79f] | 145 | xen_console_init();
|
---|
[ec04b20] | 146 |
|
---|
[0356274] | 147 | /* Enable debugger */
|
---|
| 148 | debugger_init();
|
---|
[ec04b20] | 149 |
|
---|
[0356274] | 150 | /* Merge all memory zones to 1 big zone */
|
---|
| 151 | zone_merge_all();
|
---|
| 152 | }
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[26678e5] | 155 | void arch_post_cpu_init(void)
|
---|
| 156 | {
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[0356274] | 159 | void arch_pre_smp_init(void)
|
---|
| 160 | {
|
---|
| 161 | if (config.cpu_active == 1) {
|
---|
| 162 | memory_print_map();
|
---|
| 163 |
|
---|
| 164 | #ifdef CONFIG_SMP
|
---|
| 165 | acpi_init();
|
---|
| 166 | #endif /* CONFIG_SMP */
|
---|
| 167 | }
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | void arch_post_smp_init(void)
|
---|
| 171 | {
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | void calibrate_delay_loop(void)
|
---|
| 175 | {
|
---|
[aecf79f] | 176 | // i8254_calibrate_delay_loop();
|
---|
[0356274] | 177 | if (config.cpu_active == 1) {
|
---|
| 178 | /*
|
---|
| 179 | * This has to be done only on UP.
|
---|
| 180 | * On SMP, i8254 is not used for time keeping and its interrupt pin remains masked.
|
---|
| 181 | */
|
---|
[aecf79f] | 182 | // i8254_normal_operation();
|
---|
[0356274] | 183 | }
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | /** Set thread-local-storage pointer
|
---|
| 187 | *
|
---|
| 188 | * TLS pointer is set in GS register. That means, the GS contains
|
---|
| 189 | * selector, and the descriptor->base is the correct address.
|
---|
| 190 | */
|
---|
| 191 | unative_t sys_tls_set(unative_t addr)
|
---|
| 192 | {
|
---|
| 193 | THREAD->arch.tls = addr;
|
---|
| 194 | set_tls_desc(addr);
|
---|
| 195 |
|
---|
| 196 | return 0;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | /** Acquire console back for kernel
|
---|
| 200 | *
|
---|
| 201 | */
|
---|
| 202 | void arch_grab_console(void)
|
---|
| 203 | {
|
---|
| 204 | }
|
---|
[aecf79f] | 205 |
|
---|
[0356274] | 206 | /** Return console to userspace
|
---|
| 207 | *
|
---|
| 208 | */
|
---|
| 209 | void arch_release_console(void)
|
---|
| 210 | {
|
---|
| 211 | }
|
---|
| 212 |
|
---|
| 213 | /** @}
|
---|
| 214 | */
|
---|