1# Kernel Porting 2 3 4## Porting the Chip Architecture 5 6Chip architecture porting is the basis of kernel porting. It is required when the target chip architecture is not yet supported in OpenHarmony. You can check the supported architectures in the **liteos_m/arch** directory, as shown in Table 1. 7 8 **Table 1** Architectures supported by OpenHarmony 9 10| Series| Model| 11| -------- | -------- | 12| Arm| arm9<br>cortex-m3<br>cortex-m4<br>cortex-m7<br>cortex-m33 | 13| C-SKY| v2 | 14| RISC-V| nuclei<br>riscv32 | 15| Xtensa| lx6 | 16 17 18If the target chip architecture is not yet supported in OpenHarmony, it must be adapted by the chip vendor. The **arch/include** directory contains the functions that need to be implemented for common chip architecture adaptation. Some chip architecture code is implemented by assembly, and assembly code varies with compilers. Therefore, a specific chip architecture also includes architecture code compiled by using different compilers (such as IAR, Keil, and GCC). 19 20 21 22``` 23kernel/liteos_m/arch # The path varies according to the version. 24├── arm # Arm series 25│ ├── arm9 26│ ├── cortex-m3 27│ ├── cortex-m33 28│ │ ├── gcc # Architecture code compiled by the GCC compiler 29│ │ └── iar # Architecture code compiled by the IAR compiler 30│ ├── cortex-m4 31│ ├── cortex-m7 32├── csky # C-SKY series 33├── include # Functions to be implemented for common chip architecture adaptation. 34│ ├── los_arch.h # Definition of functions required for initializing the chip architecture. 35│ ├── los_atomic.h # Definition of the atomic operation functions to be implemented by the chip architecture. 36│ ├── los_context.h # Definition of the context-related functions to be implemented by the chip architecture. 37│ ├── los_interrupt.h # Definition of the interrupt- and exception-related functions to be implemented by the chip architecture. 38│ └── los_timer.h # Definition of the system clock–related functions to be implemented by the chip architecture. 39├── risc-v # RISC-V series 40│ ├── nuclei 41│ └── riscv32 42└── xtensa # Xtensa series 43 └── lx6 44``` 45 46 47## Porting the Chip SDK 48 49Add the chip SDK to the OpenHarmony compilation framework you have set up, so as to build the file with the SDK (which does not contain the system information), which can then be burnt so that interfaces in the SDK can be called in OpenHarmony. To add the chip SDK to the OpenHarmony compilation framework, perform the following steps: 50 511. Place the chip SDK in a proper position in the **device** directory, and integrate the SDK build script and image packaging script into the compilation framework. 52 Reference build script: **device/MyDeviceCompany/MyBoard/BUILD.gn** 53 54 55 ``` 56 import("//build/lite/config/component/lite_component.gni") 57 58 executable("OHOS_Image.elf") { # Generate an executable program. 59 libs = [ 60 "xxx/xxx/libxxx.a", # Method 1 for connecting to the vendor's closed-source static library 61 ] 62 asmflags = [ # Assembly compilation parameters 63 "", 64 ] 65 ldflags = [ 66 "-T./xxx/xxx/xxx.ld", # Link script file 67 "-Lxxx/xxx/", # Static library path of the vendor. 68 "-lxxx", # Method 2 for connecting to the vendor's closed-source static library 69 "-Wl,--whole-archive", 70 "-lmodule_xxx", 71 "-Wl,--no-whole-archive", 72 ] 73 deps = [ 74 "//build/lite:ohos", # Link the dependent OpenHarmony static library after the build process is complete. 75 ":sdk", # Link the dependent static library generated from the vendor source code after the build process is complete. 76 ] 77 } 78 79 copy("prebuilt") { # Image generation tool. Generally, copy the image generation tool to the out directory. 80 sources = [ ] # Source file copied 81 outputs = [ ] # Target file copied 82 } 83 static_library("sdk") { 84 sources = [ ] # Vendor source code to compile into a static library. 85 include_dirs = [ ] # Path of the header file included in the vendor source code 86 } 87 build_ext_component("image") {# Invoke the shell command to generate an image file that can be burnt. 88 exec_path = rebase_path(root_out_dir) # Directory where Shell commands are executed 89 objcopy = "arm-none-eabi-objcopy" 90 objdump = "arm-none-eabi-objdump" 91 command = "$objcopy -O binary OHOS_Image.elf OHOS_Image.bin" 92 command += " && sh -c '$objdump -t OHOS_Image.elf | sort > OHOS_Image.sym.sorted'" 93 command += " && sh -c '$objdump -d OHOS_Image.elf > OHOS_Image.asm'" 94 deps = [ 95 ":prebuilt", # Delete this dependency if you do not need to prepare the image generation tool. 96 ":OHOS_Image.elf", # ELF file dependency 97 ] 98 } 99 group("MyBoard") { # Same as the current path 100 } 101 ``` 102 103 **Figure 1** Dependency execution sequence of targets 104 ![en-us_image_0000001378481233](figures/en-us_image_0000001378481233.png) 105 1061. Customize the **target_config.h** file. 107 Create the kernel configuration file **target_config.h** in a proper location in **device/MyDeviceCompany/MyBoard** and modify the parameter settings based on the hardware resources of the chip. For details about the parameters, see Table 2. 108 109 Reference file path: **device/hisilicon/hispark_pegasus/sdk_liteos/platform/os/Huawei_LiteOS/targets/hi3861v100/include/target_config.h** 110 111 > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** 112 > 1. If the existing configuration items do not meet the requirements, modify the **kernel/liteos_m/kernel/include/los_config.h** file as needed, which contains the full configuration of the LiteOS_M kernel. 113 > 114 > 2. Configuration items in the **target_config.h** file will overwrite those in the **los_config.h** file. 115 116 **Table 2** Main configuration items in the target_config.h file 117 118 | Configuration Item| Description| Reference Value| 119 | -------- | -------- | -------- | 120 | OS_SYS_CLOCK | System clock| 40000000UL | 121 | LOSCFG_BASE_CORE_TICK_PER_SECOND | Clock cycle of the operating system ticks.| 100UL | 122 | LOSCFG_BASE_CORE_TICK_HW_TIME | External configuration item for timer tailoring.| YES | 123 | LOSCFG_PLATFORM_HWI | Whether to use the takeover on interruption mode.| YES | 124 | LOSCFG_BASE_CORE_TSK_LIMIT | Maximum number of supported tasks (excluding idle tasks).| 32 | 125 | LOSCFG_BASE_CORE_TSK_IDLE_STACK_SIZE | Stack size of an idle task.| 0x180UL | 126 | LOSCFG_BASE_CORE_TSK_DEFAULT_STACK_SIZE | Default size of the task stack. The task stack size is 8-byte aligned.| 0x1000UL | 127 | LOSCFG_BASE_CORE_TSK_MIN_STACK_SIZE | Minimum stack size required by a task.| ALIGN(0x180, 4) | 128 | LOSCFG_BASE_CORE_TIMESLICE_TIMEOUT | Maximum execution duration of tasks with the same priority.| 2 | 129 | LOSCFG_BASE_IPC_SEM_LIMIT | Maximum number of semaphores.| 100 | 130 | LOSCFG_BASE_IPC_MUX_LIMIT | Maximum number of mutexes.| 64 | 131 | LOSCFG_BASE_IPC_QUEUE_LIMIT | Maximum number of message queues.| 64 | 132 | LOSCFG_BASE_CORE_SWTMR_LIMIT | Maximum number of supported software timers, not the number of available software timers.| 80 | 133 | LOSCFG_BASE_MEM_NODE_SIZE_CHECK | Whether to enable the memory node size check.| NO | 134 | LOSCFG_PLATFORM_EXC | Whether to enable configuration of the abnormal module.| YES | 135 | LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT | Whether to use the default interrupt of the OS.| NO | 136 1371. Change the kernel interrupt. 138 139 Use either of the following: 140 141 - Use the default interrupt of the vendor. 142 143 Set the **LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT** macro in **target_config.h** to **NO** (**0**), and modify the xxx.s startup file as follows: 144 145 - **PendSV_Handler**: interrupt entry point function provided by the vendor SDK. Replace it with the **HalPendSV** interface in OpenHarmony. 146 - **SysTick_Handler**: clock interrupt entry point function provided by the vendor SDK. Replace it with the **OsTickHandler** interface in OpenHarmony. 147 148 - Implement redirection interrupt during system initialization. 149 150 Set the **LOSCFG_USE_SYSTEM_DEFINED_INTERRUPT** and **LOSCFG_PLATFORM_HWI** macros in **target_config.h** to **YES** (**1**). 151 152 > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** 153 > 154 > The interrupt vector table **g_hwiForm** after redirection needs to be byte-aligned according to the requirements in the Arch manual. Generally, the interrupt vector table is 0x200 byte-aligned. 155 156 157## Adding the Kernel Subsystem 158 159After adding the kernel subsystem, you can compile a project with the system. To add a kernel subsystem, perform the following steps: 160 1611. Add the kernel subsystem to the **vendor/MyVendorCompany/MyProduct/config.json** file. 162 163 The sample code is as follows: 164 165 ``` 166 { 167 "subsystem": "kernel", # Kernel subsystem to add 168 "components": [ 169 { 170 "component": "liteos_m", "features":[""] 171 } 172 ] 173 }, 174 ``` 175 1762. Enable or disable kernel features. 177 178 The mini-system kernel provides the following features. This step describes how to view, enable, and disable these features. 179 180 Features: switches for the file system, backtrace, and more 181 182 Path: **kernel/liteos_m/BUILD.gn** 183 184 185 ``` 186 declare_args() { 187 enable_ohos_kernel_liteos_m_cppsupport = true # Enable CPP. 188 enable_ohos_kernel_liteos_m_cpup = true # Enable CPU usage. 189 enable_ohos_kernel_liteos_m_exchook = true # Enable exception handling. 190 enable_ohos_kernel_liteos_m_kal = true # Enable KAL interfaces. 191 enable_ohos_kernel_liteos_m_fs = true # Enable the file system. 192 enable_ohos_kernel_liteos_m_backtrace = true # Enable backtrace. 193 } 194 group("kernel") { 195 deps = [ 196 "components/bounds_checking_function:sec", 197 "kernel:kernel", 198 "utils:utils", 199 ] 200 if (enable_ohos_kernel_liteos_m_cppsupport == true) { 201 deps += [ "components/cppsupport:cppsupport" ] # If a kernel feature is set to true, the code corresponding to this feature is included in the build process. 202 } 203 ...... 204 if (enable_ohos_kernel_liteos_m_kal == true) { 205 deps += [ "kal:kal" ] 206 } 207 } 208 ``` 209 210 Features: CMSIS and POSIX support 211 212 Path: **kernel/liteos_m/kal/BUILD.gn** 213 214 215 ``` 216 declare_args() { 217 enable_ohos_kernel_liteos_m_cmsis = true # Enable CMSIS support. 218 enable_ohos_kernel_liteos_m_posix = true # Enable POSIX support. 219 } 220 static_library("kal") { 221 sources = [ "kal.c" ] 222 if (enable_ohos_kernel_liteos_m_cmsis == true) { 223 deps += [ "cmsis/" ] # If cmsis is set to true, the code in the cmsis directory is included in the build process. 224 } 225 if (enable_ohos_kernel_liteos_m_posix == true) { 226 deps += [ "posix/" ] # If posix is set to true, the code in the posix directory is included in the build process. 227 } 228 } 229 ``` 230 231 Feature: FATFS support 232 233 Path: **kernel/liteos_m/components/fs/BUILD.gn** 234 235 236 ``` 237 declare_args() { 238 enable_ohos_kernel_liteos_m_fatfs = true # Enable FATFS support. 239 } 240 group("fs") { 241 deps = [] 242 if (enable_ohos_kernel_liteos_m_fatfs == true) { 243 deps += [ "fatfs:fatfs" ] 244 } 245 } 246 ``` 247 248 > ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE** 249 > 250 > A kernel feature, such as the FS and CPP support, can be enabled or disabled in the specific product module. 251 > 252 > Path: **vendor/MyVendorCompany/MyProduct/config.json** 253 > 254 > 255 > ``` 256 > "subsystem": "kernel", 257 > "components": [ 258 > { 259 > "component": "liteos_m", 260 > "features":["enable_ohos_kernel_liteos_m_fs = false", 261 > "enable_ohos_kernel_liteos_m_cppsupport = false"] 262 > } 263 > ] 264 > } 265 > ``` 266