porting-smallchip-kernel-a.md 10.6 KB
Newer Older
E
ester.zhou 已提交
1
# LiteOS Cortex-A
D
duangavin123 已提交
2

E
ester.zhou 已提交
3
## Overview
D
duangavin123 已提交
4

E
ester.zhou 已提交
5
### Porting Scenario
D
duangavin123 已提交
6 7 8

LiteOS Cortex-A supports the ARMv7-a instruction set architecture. If you are porting the kernel to a chipset that uses ARMv7-a, you can directly perform basic adaptation. Otherwise, you need to add support for the architecture used by the chipset. This process is complex and not covered in this document.

E
ester.zhou 已提交
9
### Directory Specifications
D
duangavin123 已提交
10 11 12

For details about the LiteOS Cortex-A directory specifications, see  [LiteOS Cortex-A Overview](https://gitee.com/openharmony/kernel_liteos_a).

E
ester.zhou 已提交
13
## Adaptation Process
D
duangavin123 已提交
14 15 16

LiteOS Cortex-A provides the system initialization process and custom configuration options required for system running. During porting, pay attention to the functions related to hardware configuration in the initialization process.

D
duangavin123 已提交
17
The LiteOS Cortex-A initialization process consists of seven steps:
D
duangavin123 已提交
18

E
ester.zhou 已提交
19 20 21 22 23 24 25
1.  Add the  **target\_config.h** file and compile the macros **DDR\_MEM\_ADDR** and **DDR\_MEM\_SIZE**, which indicate the start address and length of the board memory, respectively. The prelinker script **board.ld.S** creates the linker script **board.ld** based on the two macros.
2. Define **g\_archMmuInitMapping**, the global array of MMU mappings, to specify the memory segment attributes and the virtual-to-physical address mappings. The memory mapping will be established based on this array during kernel startup.
3. If there are multiple cores, define **struct SmpOps**, the handle to the secondary core operation function. The **SmpOps-\>SmpCpuOn** function needs to implement the feature of waking up a secondary core. Then, define the **SmpRegFunc** function and call the **LOS\_SmpOpsSet** interface to register the handle. The registration process is completed by starting the framework using **LOS\_MODULE\_INIT\(SmpRegFunc, LOS\_INIT\_LEVEL\_EARLIEST\)**.
4. Create a kernel image based on the linker script **board.ld**.
5. Perform operations such as initialization of the interrupt vector table and MMU page table are performed in the assembly files: **reset\_vector\_up.S** and **reset\_vector\_mp.S**, from which a single-core CPU and a multi-core CPU start, respectively.
6. Enable the assembly code in **reset\_vector.S** to jump to the **main** function of the C programming language to initialize the hardware clock, software timer, memory, and tasks. This process depends on the feature macro configuration in **target\_config.h**. Then, create the **SystemInit** task to be implemented in the board code, with **OsSchedStart\(\)** enabled for task scheduling.
7. Call the **DeviceManagerStart** function to initialize the HDF driver. This process is implemented by calling the driver configuration file **hdf.hcs** and drivers source code in the board code.
D
duangavin123 已提交
26

D
duangavin123 已提交
27
The figure below shows the overall initialization process.
D
duangavin123 已提交
28

E
ester.zhou 已提交
29
**Figure 1** Overall initialization process
E
ester.zhou 已提交
30
![](figures/overall-initialization-process.png "overall-initialization-process")
D
duangavin123 已提交
31 32 33

As can be seen from preceding figure, kernel basic adaptation involves the following parts:

E
ester.zhou 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
- Adding the **target\_config.h** file, which contains board hardware parameters and feature parameters described in the following table:

  **Table 1** Parameters in the target\_config.h file

  | Parameter               | Description                                                 |
  | ----------------------- | ----------------------------------------------------------- |
  | OS_SYS_CLOCK            | System cycle frequency                                      |
  | DDR_MEM_ADDR            | Start address of the system memory                          |
  | DDR_MEM_SIZE            | Size of the system memory                                   |
  | PERIPH_PMM_BASE         | Base address of the peripheral register                     |
  | PERIPH_PMM_SIZE         | Size of the peripheral register                             |
  | OS_HWI_MIN              | Minimum number of system interrupts                         |
  | OS_HWI_MAX              | Maximum number of system interrupts                         |
  | NUM_HAL_INTERRUPT_UART0 | UART0 interrupt ID                                          |
  | UART0_REG_BASE          | UART0 register base address                                 |
  | GIC_BASE_ADDR           | Base address of the GIC interrupt register                  |
  | GICD_OFFSET             | Offset address of the GICD relative to the GIC base address |
  | GICC_OFFSET             | Offset address of the GICC relative to the GIC base address |

  

- Implementing the  **SystemInit**  function to initialize services in the user space. Figure 2 shows a typical initialization scenario.

  **Figure  2**  Service startup process

  ![](figures/service-startup-process.png "service-startup-process")
D
duangavin123 已提交
60

D
duangavin123 已提交
61
-   Implementing the  **main**  function for basic kernel initialization and initialization of services in the board kernel space.  [Figure 3](#fig32611728133919)  shows the initialization process, where the kernel startup framework takes the lead in the initialization process. The light blue part in the figure indicates the phase in which external modules can be registered and started in the startup framework.
D
duangavin123 已提交
62

E
ester.zhou 已提交
63
    >![](../public_sys-resources/icon-caution.gif) **CAUTION** 
D
duangavin123 已提交
64 65
    >Modules at the same layer cannot depend on each other.

E
ester.zhou 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    **Figure  3**  Kernel startup framework
    
![](figures/kernel-startup-framework.jpg "kernel-startup-framework")
    
**Table  2**  Startup framework layers
    
    | Layer                         | Description                                                  |
    | ----------------------------- | ------------------------------------------------------------ |
    | LOS_INIT_LEVEL_EARLIEST       | Earliest initialization.<br>This layer does not depend on the architecture. The board and subsequent modules, such as the Trace module, will initialize the software-only modules on which they depend. |
    | LOS_INIT_LEVEL_ARCH_EARLY     | Early initialization of the architecture.<br/>This layer depends on the architecture. Subsequent modules will initialize the modules on which they depend. It is recommended that functions not required for startup be placed at the **LOS_INIT_LEVEL_ARCH** layer. |
    | LOS_INIT_LEVEL_PLATFORM_EARLY | Early initialization of the platform.<br/>This layer depends on the board platform and drivers. Subsequent modules will initialize the modules on which they depend. It is recommended that functions required for startup be placed at the **LOS_INIT_LEVEL_PLATFORM** layer.<br/>Example: UART module |
    | LOS_INIT_LEVEL_KMOD_PREVM     | Kernel module initialization before memory initialization.<br/>This layer involves initialization of the modules that need to be enabled before memory initialization. |
    | LOS_INIT_LEVEL_VM_COMPLETE    | Initialization after the basic memory is ready.<br/>This layer involves initialization of the modules that need to be enabled and do not depend on the inter-process communication mechanism and system processes.<br/>Example: shared memory function |
    | LOS_INIT_LEVEL_ARCH           | Late initialization of the architecture.<br/>This layer depends on the architecture extension function. Subsequent modules will initialize the modules on which they depend. |
    | LOS_INIT_LEVEL_PLATFORM       | Late initialization of the platform.<br/>This layer depends on the board platform and drivers. Subsequent modules will initialize the modules on which they depend.<br/>Example: initialization of the driver kernel abstraction layer (MMC and MTD) |
    | LOS_INIT_LEVEL_KMOD_BASIC     | Initialization of the kernel basic modules.<br/>This layer is used to initialize the basic modules that can be detached from the kernel.<br/>Example: VFS initialization |
    | LOS_INIT_LEVEL_KMOD_EXTENDED  | Initialization of the kernel extended modules.<br/>This layer is used to initialize the extended modules that can be detached from the kernel.<br/>Example: system call initialization, ProcFS initialization, Futex initialization, HiLog initialization, HiEvent initialization, and LiteIPC initialization |
    | LOS_INIT_LEVEL_KMOD_TASK      | Kernel task creation.<br/>This layer can be used to create kernel tasks (kernel thread and software timer tasks).<br/>Example: creation of the resident resource reclaiming task, SystemInit task, and CPU usage statistics task |
    
    
    
D
duangavin123 已提交
87
    Adaptation for board porting. Focus on layers between  **LOS\_INIT\_LEVEL\_ARCH**  and  **LOS\_INIT\_LEVEL\_KMOD\_TASK**  and try to divide the initialization process into as many phases as possible for refined registration.
E
ester.zhou 已提交
88 89
    
    >![](../public_sys-resources/icon-note.gif) **NOTE** 
D
duangavin123 已提交
90 91 92 93
    >Modules at the same layer cannot depend on each other. It is recommended that a new module be split based on the preceding startup phase and be registered and started as required.
    >You can view the symbol table in the  **.rodata.init.kernel.\***  segment of the  **OHOS\_Image.map**  file generated after the build is complete, so as to learn about the initialization entry of each module that has been registered with the kernel startup framework and check whether the newly registered initialization entry takes effect.


E
ester.zhou 已提交
94
## Programming Example
D
duangavin123 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

In the board SDK file:

```
/* Header file of the kernel startup framework */
#include "los_init.h"
......

/* Initialization function of the new module */
unsigned int OsSampleModInit(void)
{
    PRINTK("OsSampleModInit SUCCESS!\n");
    ......
}
......
/* Register the new module at the target layer of the startup framework. */
LOS_MODULE_INIT(OsSampleModInit, LOS_INIT_LEVEL_KMOD_EXTENDED);
```

E
ester.zhou 已提交
114
## Verification
D
duangavin123 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

```
main core booting up...
OsSampleModInit SUCCESS!
releasing 1 secondary cores
cpu 1 entering scheduler
cpu 0 entering scheduler
```

According to the preceding information displayed during the system startup, the kernel calls the initialization function of the registered module during the startup to initialize the module.

The system enters the kernel-space shell and the task commands can be properly executed.

```
OHOS # help
E
ester.zhou 已提交
130
***shell commands:*
D
duangavin123 已提交
131 132 133 134 135 136 137 138

arp           cat           cd            chgrp         chmod         chown         cp            cpup          
date          dhclient      dmesg         dns           format        free          help          hwi           
ifconfig      ipdebug       kill          log           ls            lsfd          memcheck      mkdir         
mount         netstat       oom           partinfo      partition     ping          ping6         pmm           
pwd           reset         rm            rmdir         sem           shm           stack         statfs        
su            swtmr         sync          systeminfo    task          telnet        touch         umount        
uname         v2p           virstatfs     vmm           watch         writeproc     
D
duangavin123 已提交
139

D
duangavin123 已提交
140 141
```