FreeNOS
Main.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2019 Niek Linnenbank
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <FreeNOS/Config.h>
19 #include <FreeNOS/Support.h>
20 #include <FreeNOS/System.h>
21 #include <arm/ARMPaging.h>
22 #include <arm/ARMControl.h>
23 #include <arm/ARMCore.h>
24 #include <NS16550.h>
25 #include <DeviceLog.h>
26 #include <SunxiCoreServer.h>
27 #include <Macros.h>
28 #include <MemoryBlock.h>
29 #include <Memory.h>
30 #include "SunxiKernel.h"
31 
33 
34 static u32 ALIGN(16 * 1024) SECTION(".data") tmpPageDir[4096];
35 
36 extern C int kernel_main(void)
37 {
38 #ifdef ARMV7
39  // Raise the SMP bit for ARMv7
40  ARMControl ctrl;
41  ctrl.set(ARMControl::SMPBit);
42 #endif
43 
44  if (read_core_id() == 0)
45  {
46  // Invalidate all caches now
47  Arch::Cache cache;
49  }
50 
51  // Setup memory map with the memory base physical memory address
52  Arch::MemoryMap mem;
53  Address memoryBaseAddr = RAM_ADDR;
54 
55  if (read_core_id() != 0) {
56  CoreInfo tmpInfo;
57  MemoryBlock::copy((void *)&tmpInfo,
59  memoryBaseAddr = tmpInfo.memory.phys;
60  }
61 
62  // Prepare early page tables and re-map the temporary stack
63  ARMPaging paging(&mem, (Address) &tmpPageDir, memoryBaseAddr);
64 
65  // Activate MMU
66  paging.initialize();
67  paging.activate(true);
68 
69  // Fill coreInfo for boot core
70  if (read_core_id() == 0)
71  {
72  BootImage *bootimage = (BootImage *) &__bootimg;
73  MemoryBlock::set(&coreInfo, 0, sizeof(CoreInfo));
74  coreInfo.bootImageAddress = (Address) (bootimage);
78  coreInfo.memory.phys = RAM_ADDR;
79  coreInfo.memory.size = RAM_SIZE;
80  }
81  // Copy CoreInfo prepared by the CoreServer
82  else
83  {
84  MemoryBlock::copy((void *)&coreInfo,
86  }
87 
88  // Clear BSS
89  clearBSS();
90 
91  // Initialize heap
93 
94  // Run all constructors first
95  constructors();
96 
97  // Open serial console as default Log
98  NS16550 *uart = new NS16550(UART0_IRQ);
99  uart->initialize();
100  DeviceLog *console = new DeviceLog(*uart);
101 
102  // Only the boot core outputs notifications
103  if (read_core_id() == 0)
105  else
107 
108  // Create the kernel
109  SunxiKernel *kernel = new SunxiKernel(&coreInfo);
110 
111  // Run the kernel
112  return kernel->run();
113 }
ARMPaging::activate
virtual Result activate(bool initializeMMU=false)
Activate the MemoryContext.
Definition: ARMPaging.cpp:198
DeviceLog
Generic logger that writes to a Device object.
Definition: DeviceLog.h:38
MemoryBlock::copy
static Size copy(void *dest, const void *src, Size count)
Copy memory from one place to another.
Definition: MemoryBlock.cpp:36
Macros.h
Log::setMinimumLogLevel
void setMinimumLogLevel(Level level)
Set the minimum logging level.
Definition: Log.cpp:38
CoreInfo::bootImageSize
Address bootImageSize
Boot image size in bytes.
Definition: CoreInfo.h:84
MemoryBlock::set
static void * set(void *dest, int ch, unsigned count)
Fill memory with a constant byte.
Definition: MemoryBlock.cpp:25
__end
Address __end
Definition: Main.cpp:31
ARMPaging
ARM virtual memory implementation.
Definition: ARMPaging.h:43
NS16550.h
read_core_id
#define read_core_id()
Read unique core identifier.
Definition: ARMCore.h:145
Kernel::initializeHeap
static Error initializeHeap()
Initialize heap.
Definition: Kernel.cpp:108
ARMPaging.h
ARMCore.h
Address
unsigned long Address
A memory address.
Definition: Types.h:131
CoreInfo::memory
Memory::Range memory
Defines the physical memory available to the core.
Definition: CoreInfo.h:69
ARMPaging::initialize
virtual Result initialize()
Initialize the MemoryContext.
Definition: ARMPaging.cpp:55
Kernel::run
int run()
Execute the kernel.
Definition: Kernel.cpp:392
SunxiCoreServer.h
MemoryBlock.h
kernel_main
C int kernel_main(void)
Definition: Main.cpp:35
ALIGN
static u32 ALIGN(16 *1024) SECTION(".data") tmpPageDir[4096]
BootImage::bootImageSize
u32 bootImageSize
Total size of the boot image in bytes.
Definition: BootImage.h:53
ARMMap
Memory mapping for the kernel and user processes on the ARM architecture.
Definition: ARMMap.h:37
C
#define C
Used to define external C functions.
Definition: Macros.h:134
SunxiKernel.h
ARMCacheV6::invalidate
virtual Result invalidate(Type type)
Invalidate the entire cache.
Definition: ARMCacheV6.cpp:21
ARMControl
ARM System Control Coprocessor (CP15).
Definition: ARMControl.h:47
Memory::Range::phys
Address phys
Physical address.
Definition: Memory.h:58
SunxiCoreServer::SecondaryCoreInfoAddress
static const Address SecondaryCoreInfoAddress
Physical memory address for CoreInfo passed to secondary cores during bootup.
Definition: SunxiCoreServer.h:43
__start
Address __start
ARMCacheV6
ARMv6 cache management implementation.
Definition: ARMCacheV6.h:42
u32
unsigned int u32
Unsigned 32-bit number.
Definition: Types.h:53
CoreInfo::bootImageAddress
Address bootImageAddress
Boot image physical memory address.
Definition: CoreInfo.h:81
Log::Notice
@ Notice
Definition: Log.h:113
constructors
C void constructors()
Invokes all function pointers inside the .ctors section.
Definition: Support.cpp:22
CoreInfo
Per-Core information structure.
Definition: CoreInfo.h:60
Memory.h
coreInfo
CoreInfo coreInfo
Local CoreInfo instance.
DeviceLog.h
__bootimg
Address __bootimg
Definition: Main.cpp:31
SECTION
#define SECTION(s)
Can be used to link a symbol inside a specific section.
Definition: Macros.h:145
ARMControl::set
void set(SystemControlFlags flags)
Set system control flags in CP15.
Definition: ARMControl.cpp:88
CoreInfo::kernel
Memory::Range kernel
Kernel memory range.
Definition: CoreInfo.h:75
SunxiKernel
Represents the Sunxi kernel implementation.
Definition: SunxiKernel.h:36
NS16550::initialize
virtual FileSystem::Result initialize()
Initializes the UART.
Definition: NS16550.cpp:33
Log::Warning
@ Warning
Definition: Log.h:112
ARMControl.h
BootImage
BootImage contains executable programs to be loaded at system bootup.
Definition: BootImage.h:44
Cache::Unified
@ Unified
Definition: Cache.h:57
clearBSS
void clearBSS()
Generic function to clear the BSS memory section to zero.
Definition: Memory.cpp:21
Memory::Range::size
Size size
Size in number of bytes.
Definition: Memory.h:59
ARMControl::SMPBit
@ SMPBit
Definition: ARMControl.h:105
NS16550
The NS16550 is a commonly available UART device.
Definition: NS16550.h:38