1/*
2 * Copyright 2013, winocm. <winocm@icloud.com>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 *   Redistributions of source code must retain the above copyright notice, this
9 *   list of conditions and the following disclaimer.
10 *
11 *   Redistributions in binary form must reproduce the above copyright notice, this
12 *   list of conditions and the following disclaimer in the documentation and/or
13 *   other materials provided with the distribution.
14 *
15 *   If you are going to use this software in any form that does not involve
16 *   releasing the source to this project or improving it, let me know beforehand.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
22 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29/*
30 * ARM VM initialization
31 */
32
33#include <mach_debug.h>
34#include <debug.h>
35#include <mach/vm_types.h>
36#include <mach/vm_param.h>
37#include <mach/thread_status.h>
38#include <kern/misc_protos.h>
39#include <kern/assert.h>
40#include <kern/cpu_number.h>
41#include <kern/thread.h>
42#include <console/serial_protos.h>
43#include <libkern/kernel_mach_header.h>
44#include <arm/pmap.h>
45#include <arm/misc_protos.h>
46
47/*
48 * cpu_ttb contains the current TTB (translation-table
49 * base) of the processor. This is a physical address.
50 */
51uint64_t    cpu_ttb;
52
53/*
54 * The section offset is set by the bootloader.
55 */
56uint32_t    sectionOffset = 0x2000;
57
58/*
59 * sane_size, max_mem and mem_size are controlled by arm_vm_init
60 *
61 * At the moment, sane_size is forced to the size of memory
62 * the booter passes to the kernel.
63 */
64uint64_t    sane_size   = 0;
65vm_size_t   mem_size    = 0;
66
67/*
68 * The physical and virtual base of the kernel. These are used in
69 */
70unsigned long gPhysBase = 0x0, gVirtBase = 0x0;
71unsigned long gTopOfKernel;
72
73addr64_t vm_last_addr = VM_MAX_KERNEL_ADDRESS;
74
75/*
76 * Break down the KASLR. (pronunced like "castle" but with a twangy accent).
77 */
78
79/*
80 * These variables are initialized during vm init.
81 */
82ppnum_t		vm_kernel_base_page;
83vm_offset_t	vm_kernel_base;
84vm_offset_t	vm_kernel_top;
85vm_offset_t	vm_kernel_stext;
86vm_offset_t	vm_kernel_etext;
87vm_offset_t	vm_kernel_slide;
88
89/*
90 * These both are initialized to the same value.
91 */
92uint64_t        max_mem;
93uint64_t        mem_actual;
94
95/*
96 * The sectXxx stuff contains the current kernel Mach-O section information.
97 */
98vm_offset_t sectTEXTB;
99unsigned long sectSizeTEXT;
100vm_offset_t sectDATAB;
101unsigned long sectSizeDATA;
102vm_offset_t sectLINKB;
103unsigned long sectSizeLINK;
104vm_offset_t sectKLDB;
105unsigned long sectSizeKLD;
106vm_offset_t sectPRELINKB;
107unsigned long sectSizePRELINK;
108vm_offset_t sectHIBB;
109unsigned long sectSizeHIB;
110
111vm_offset_t end, etext, edata;
112
113extern void* ExceptionVectorsBase;
114
115/*
116 * These both represent the first physical page we can use in the system,
117 * and the end of the physical memory region.
118 */
119uint64_t first_avail;
120uint64_t avail_end;
121