xref: /xnu-12377.81.4/tests/unit/mocks/fake_kinit.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1 /*
2  * Copyright (c) 2000-2024 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 
29 #include <arm/cpu_data_internal.h> // for BootArgs
30 #include <pexpert/pexpert.h>  // for PE_state
31 #include <pexpert/boot.h>  // for bootargs
32 #include <kern/startup.h>  // for kernel_startup_initialize_upto
33 #include <IOKit/IOLocks.h> // for IOLock
34 #include <vm/vm_page_internal.h> // for vm_set_page_size
35 #include <kern/clock.h> // for clock_config
36 #include <vm/pmap.h>
37 
38 #include "std_safe.h"
39 
40 // This define is supposed to come from the .CFLAGS parsing. if it's not, something is wrong with the Makefile
41 #ifndef __BUILDING_XNU_LIB_UNITTEST__
42 #error "not building unittest, something is wrong"
43 #endif
44 
45 
46 extern void kernel_startup_bootstrap(void);
47 extern void scale_setup(void);
48 extern void vm_mem_bootstrap(void);
49 extern void waitq_bootstrap(void);
50 // can't include IOKit/IOKitDebug.h since it's a C++ file
51 extern void IOTrackingInit(void);
52 extern void mock_mem_init_vm_objects(void);
53 
54 extern lck_grp_t * IOLockGroup;
55 extern IOLock * sKextLoggingLock;
56 extern bitmap_t * asid_bitmap;
57 extern zone_t pmap_zone;
58 
59 void
fake_pmap_init(void)60 fake_pmap_init(void)
61 {
62 	pmap_zone = zone_create_ext("pmap", sizeof(struct pmap),
63 	    ZC_ZFREE_CLEARMEM, ZONE_ID_PMAP, NULL);
64 
65 	static uint64_t asid_bits = 0;
66 	asid_bitmap = &asid_bits;
67 }
68 
69 
70 void
fake_init_bootargs(void)71 fake_init_bootargs(void)
72 {
73 	// see PE_boot_args()
74 	static boot_args ba;
75 	PE_state.bootArgs = &ba;
76 	PE_state.initialized = TRUE;
77 	BootArgs = &ba; // arm_init()
78 }
79 
80 void
fake_kernel_bootstrap(void)81 fake_kernel_bootstrap(void)
82 {
83 	mem_size = 0x0000000080000000ULL; // 2 GB
84 	max_mem = mem_size;
85 	scale_setup();
86 
87 	vm_set_page_size(); // called from arm_init() -> arm_vm_init()
88 	vm_mem_bootstrap();
89 	fake_pmap_init();
90 	clock_config();
91 }
92 
93 
94 void
fake_iokit_init(void)95 fake_iokit_init(void)
96 {
97 	// these are needed for static initializations in iokit to not crash
98 	IOLockGroup = lck_grp_alloc_init("IOKit", LCK_GRP_ATTR_NULL);
99 #if IOTRACKING
100 	IOTrackingInit();
101 #endif
102 	sKextLoggingLock = IOLockAlloc();
103 }
104 
105 // This is the first function that is called before any initialization in libkernel.
106 // It's made to be first by the order of object files in the linker command line in Makefile
107 __attribute__((constructor)) void
fake_kinit(void)108 fake_kinit(void)
109 {
110 	fake_init_bootargs();
111 	kernel_startup_bootstrap();
112 	fake_kernel_bootstrap();
113 	fake_iokit_init();
114 
115 	kernel_startup_initialize_only(STARTUP_SUB_MACH_IPC);
116 	kernel_startup_initialize_only(STARTUP_SUB_SYSCTL);
117 
118 	mock_mem_init_vm_objects();
119 }
120