1# 内核子系统<a name="ZH-CN_TOPIC_0000001077309884"></a>
2
3-   [简介](#section11660541593)
4-   [LiteOS](#section6253122153515)
5-   [Linux](#section143373618411)
6-   [目录](#section21571344112)
7-   [约束](#section19647171710417)
8-   [使用](#section1393789267)
9    -   [LiteOS使用说明](#section118811457303)
10    -   [linux使用说明](#section1352114469620)
11
12-   [构建说明](#section19369206113115)
13-   [相关仓](#section27639463106)
14
15## 简介<a name="section11660541593"></a>
16
17OpenHarmony针对不同量级的系统,分别使用了不同形态的内核,分别为LiteOS和Linux。在轻量系统、小型系统可以选用LiteOS;在小型系统和标准系统上可以选用Linux。
18
19<a name="table91002058194612"></a>
20<table><thead align="left"><tr id="row010015589464"><th class="cellrowborder" valign="top" width="25%" id="mcps1.2.5.1.1"><p id="p310015824612"><a name="p310015824612"></a><a name="p310015824612"></a>系统级别</p>
21</th>
22<th class="cellrowborder" valign="top" width="25%" id="mcps1.2.5.1.2"><p id="p910013586463"><a name="p910013586463"></a><a name="p910013586463"></a>轻量系统</p>
23</th>
24<th class="cellrowborder" valign="top" width="25%" id="mcps1.2.5.1.3"><p id="p14100858164615"><a name="p14100858164615"></a><a name="p14100858164615"></a>小型系统</p>
25</th>
26<th class="cellrowborder" valign="top" width="25%" id="mcps1.2.5.1.4"><p id="p191001158154610"><a name="p191001158154610"></a><a name="p191001158154610"></a>标准系统</p>
27</th>
28</tr>
29</thead>
30<tbody><tr id="row18100165894619"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.1 "><p id="p110055824611"><a name="p110055824611"></a><a name="p110055824611"></a>LiteOS</p>
31</td>
32<td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.2 "><p id="p3100175815461"><a name="p3100175815461"></a><a name="p3100175815461"></a>√</p>
33</td>
34<td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.3 "><p id="p15762194124714"><a name="p15762194124714"></a><a name="p15762194124714"></a>√</p>
35</td>
36<td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.4 "><p id="p647872125416"><a name="p647872125416"></a><a name="p647872125416"></a>×</p>
37</td>
38</tr>
39<tr id="row15104331164711"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.1 "><p id="p15104163120477"><a name="p15104163120477"></a><a name="p15104163120477"></a>Linux</p>
40</td>
41<td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.2 "><p id="p15762194124714"><a name="p15762194124714"></a><a name="p15762194124714"></a>×</p>
42<td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.3 "><p id="p15762194124714"><a name="p15762194124714"></a><a name="p15762194124714"></a>√</p>
43<td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.4 "><p id="p4251543134711"><a name="p4251543134711"></a><a name="p4251543134711"></a>√</p>
44</td>
45</tr>
46</tbody>
47</table>
48
49## LiteOS<a name="section6253122153515"></a>
50
51OpenHarmony LiteOS内核是面向IoT领域的实时操作系统内核,它同时具备RTOS轻快和Linux易用的特点。
52
53OpenHarmony LiteOS内核主要包括进程和线程调度、内存管理、IPC机制、timer管理等内核基本功能。
54
55OpenHarmony LiteOS内核的源代码分为 kernel\_liteos\_a 和 kernel\_liteos\_m 这2个代码仓库,其中kernel\_liteos\_a主要针对小型系统(small system)和标准系统(standard system),而kernel\_liteos\_m则主要针对轻量系统(mini system),下面主要针对kernel\_liteos\_a代码仓库进行介绍。图1为OpenHarmony LiteOS-A内核架构图:
56
57**图 1**  OpenHarmony LiteOS-A内核架构图<a name="fig225412228353"></a>
58![](figures/OpenHarmony-LiteOS-A内核架构图.png "OpenHarmony-LiteOS-A内核架构图")
59
60## Linux<a name="section143373618411"></a>
61
62OpenHarmony的Linux内核基于开源Linux内核LTS **4.19.y / 5.10.y / 6.6.y** 分支演进,在此基线基础上,回合CVE补丁及OpenHarmony特性,作为OpenHarmony Common Kernel基线。针对不同的芯片,各厂商合入对应的板级驱动补丁,完成对OpenHarmony的基线适配。
63
64Linux社区LTS 4.19.y分支信息请查看[kernel官网](https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/log/?h=linux-4.19.y)65
66Linux社区LTS 5.10.y分支信息请查看[kernel官网](https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/log/?h=linux-5.10.y)67
68Linux社区LTS 6.6.y分支信息请查看[kernel官网](https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/log/?h=linux-6.6.y)69
70内核的Patch组成模块,在编译构建流程中,针对具体芯片平台,合入对应的架构驱动代码,进行编译对应的内核镜像。所有补丁来源均遵守GPL-2.0协议。
71
72## 目录<a name="section21571344112"></a>
73
74```
75kernel/
76├── linux
77│   ├── common_modules                     # linux内核通用模块仓
78│   ├── linux-4.19                         # OpenHarmony linux-4.19 Common kernel
79│   ├── linux-5.10                         # OpenHarmony linux-5.10 Common kernel
80│   ├── linux-6.6                          # OpenHarmony linux-6.6  Common kernel
81│   ├── build
82│   │   ├── BUILD.gn                       # 编译框架GN文件
83│   │   ├── kernel.mk                      # 内核编译文件
84│   │   └── ohos.build                     # 内核编译组件文件
85│   ├── patches
86│   │   ├── linux-4.19                     # linux-4.19 相关patch
87│   │   │   ├── common_patch
88│   │   │   │   └── hdf.patch              # linux-4.19 HDF patches
89│   │   │   └── hispark_taurus_patch
90│   │   │       └── hispark_taurus.patch   # linux-4.19 hispark_taurus SOC patches
91│   │   ├── linux-5.10
92│   │   │   ├── common_patch
93│   │   │   │   └── hdf.patch              # linux-5.10 HDF patches
94│   │   │   └── hispark_taurus_patch
95│   │   │   │   └── hispark_taurus.patch   # linux-5.10 hispark_taurus SOC patches
96│   │   │   └── rk3568_patch
97│   │   │       ├── kernel.patch           # linux-5.10 rk3568 SOC patches
98│   │   │       └── hdf.patch              # linux-5.10 rk3568 定制 HDF patches
99│   │   └── linux-6.6
100│   │       └── rk3568_patch
101│   │           ├── kernel.patch           # linux-6.6  rk3568 SOC patches
102│   │           └── hdf.patch              # linux-6.6  rk3568 定制 HDF patches
103│   └── config
104│       ├── linux-4.19
105│       │   └── arch
106│       │       └── arm
107│       │          └── configs
108│       │              ├── hispark_taurus_small_defconfig        # 厂商Hisilicon对应的开源开发板hispark_taurus小型系统的defconfig
109│       │              ├── hispark_taurus_standard_defconfig     # 厂商Hisilicon对应的开源开发板hispark_taurus标准系统的defconfig
110│       │              ├── small_common_defconfig                # 小型系统的内核的common defconfig
111│       │              └── standard_common_defconfig             # 标准系统的内核的common defconfig
112│       └── linux-5.10 or linux-6.6
113│           ├── base_defconfig                                # 内核必选或安全红线模块基础配置
114│           ├── type                                          # 形态配置目录
115│           │   ├── small_defconfig                           # 小型系统常用配置文件
116│           │   └── standard_defconfig                        # 标准系统常用配置文件
117│           ├── form                                          # 版本配置目录
118│           │   └── debug_defconfig                           # 调试版本配置文件(示例目前无配置需求)
119│           ├── rk3568                                        # 厂商平台配置目录
120│           │   └── arch
121│           │       └── arm64_defconfig                       # 芯片单板64位版本相关配置文件
122│           └── product                                       # 相关产品类型配置目录
123│               └── phone_defconfig                           # 手机类型产品相关配置文件
124└── liteos_a               # liteos内核基线代码
125    ├── apps               # 用户态的init和shell应用程序
126    ├── arch               # 体系架构的目录,如arm等
127    │   └── arm            # arm架构代码
128    ├── bsd                # freebsd相关的驱动和适配层模块代码引入,例如USB等
129    ├── compat             # 内核接口兼容性目录
130    │   └── posix          # posix相关接口
131    ├── drivers            # 内核驱动
132    │   └── char           # 字符设备
133    │       ├── mem        # 访问物理IO设备驱动
134    │       ├── quickstart # 系统快速启动接口目录
135    │       ├── random     # 随机数设备驱动
136    │       └── video      # framebuffer驱动框架
137    ├── fs                 # 文件系统模块,主要来源于NuttX开源项目
138    │   ├── fat            # fat文件系统
139    │   ├── jffs2          # jffs2文件系统
140    │   ├── include        # 对外暴露头文件存放目录
141    │   ├── nfs            # nfs文件系统
142    │   ├── proc           # proc文件系统
143    │   ├── ramfs          # ramfs文件系统
144    │   └── vfs            # vfs层
145    ├── kernel             # 进程、内存、IPC等模块
146    │   ├── base           # 基础内核,包括调度、内存等模块
147    │   ├── common         # 内核通用组件
148    │   ├── extended       # 扩展内核,包括动态加载、vdso、liteipc等模块
149    │   ├── include        # 对外暴露头文件存放目录
150    │   └── user           # 加载init进程
151    ├── lib                # 内核的lib库
152    ├── net                # 网络模块,主要来源于lwip开源项目
153    ├── platform           # 支持不同的芯片平台代码,如hispark_taurus等
154    │   ├── hw             # 时钟与中断相关逻辑代码
155    │   ├── include        # 对外暴露头文件存放目录
156    │   └── uart           # 串口相关逻辑代码
157    ├── platform           # 支持不同的芯片平台代码,如hispark_taurus等
158    ├── security           # 安全特性相关的代码,包括进程权限管理和虚拟id映射管理
159    ├── syscall            # 系统调用
160    └── tools              # 构建工具及相关配置和代码
161```
162
163## 约束<a name="section19647171710417"></a>
164
165LiteOS:
166
167Hi3518EV300默认使用jffs2文件系统,hispark_taurus默认使用FAT文件系统。若要使用其他文件系统,需要新增适配。
168
169## 使用<a name="section1393789267"></a>
170
171### LiteOS使用说明<a name="section118811457303"></a>
172
173参见LiteOS-A内核[README](https://gitee.com/openharmony/kernel_liteos_a/blob/master/README_zh.md)和LiteOS-M内核[README](https://gitee.com/openharmony/kernel_liteos_m/blob/master/README_zh.md)的“使用说明”章节。
174
175### linux使用说明<a name="section1352114469620"></a>
176
1771. 合入HDF补丁
178
179kernel/linux/build仓中,按照kernel.mk中HDF的补丁合入方法,合入不同内核版本对应的HDF内核补丁:
180
181	```
182	$(OHOS_BUILD_HOME)/drivers/hdf_core/adapter/khdf/linux/patch_hdf.sh $(OHOS_BUILD_HOME) $(KERNEL_SRC_TMP_PATH) $(KERNEL_PATCH_PATH) $(DEVICE_NAME)
183	```
184
1852. 合入芯片平台驱动补丁
186
187	以hispark_taurus为例:
188
189kernel/linux/build仓中,按照kernel.mk中的芯片组件所对应的patch路径规则及命名规则,将对应的芯片组件patch放到对应路径下:
190
191	```
192	DEVICE_PATCH_DIR := $(OHOS_BUILD_HOME)/kernel/linux/patches/${KERNEL_VERSION}/$(DEVICE_NAME)_patch
193	DEVICE_PATCH_FILE := $(DEVICE_PATCH_DIR)/$(DEVICE_NAME).patch
194	```
195
1963. 修改自己所需要编译的config
197
198kernel/linux/build仓中,按照kernel.mk中的芯片组件所对应的patch路径规则及命名规则,将对应的芯片组件config放到对应路径下:
199
200	```
201	KERNEL_CONFIG_PATH := $(OHOS_BUILD_HOME)/kernel/linux/config/${KERNEL_VERSION}
202	DEFCONFIG_FILE := $(DEVICE_NAME)_$(BUILD_TYPE)_defconfig
203	```
204
205	> **须知:**
206	>
207	>由于OpenHarmony工程的编译构建流程中会拷贝kernel/linux/linux-\*\.\*的代码环境后进行打补丁动作,在使用OpenHarmony的版本级编译命令前,需要kernel/linux/linux-\*\.\*原代码环境。
208	>
209	>根据不同系统工程,编译完成后会在out目录下的kernel目录中生成对应实际编译的内核,基于此目录的内核,进行对应的config修改,将最后生成的\.config文件cp到config仓对应的路径文件里,即可生效。
210
211## 构建说明<a name="section19369206113115"></a>
212
213以hispark_taurus开源开发板+ubuntu x86主机开发环境为例
214
215使用工程的全量编译命令,编译生成uImage内核镜像
216
217```
218./build.sh --product-name rk3568					# 编译rk3568镜像
219    --gn-args linux_kernel_version="linux-6.6"		# 编译指定内核版本
220```
221
222## 相关仓<a name="section27639463106"></a>
223
224**内核子系统**
225
226LiteOS:
227
228[drivers\_liteos](https://gitee.com/openharmony/drivers_liteos/blob/master/README_zh.md)
229
230[kernel\_liteos\_a](https://gitee.com/openharmony/kernel_liteos_a/blob/master/README_zh.md)
231
232[kernel\_liteos\_m](https://gitee.com/openharmony/kernel_liteos_m/blob/master/README_zh.md)
233
234[device\_qemu](https://gitee.com/openharmony/device_qemu/blob/master/README_zh.md)
235
236Linux:
237
238[kernel\_linux\_patches](https://gitee.com/openharmony/kernel_linux_patches/blob/master/README_zh.md)
239
240[kernel\_linux\_config](https://gitee.com/openharmony/kernel_linux_config/blob/master/README_zh.md)
241
242[kernel\_linux\_build](https://gitee.com/openharmony/kernel_linux_build/blob/master/README_zh.md)
243
244[kernel\_linux\_5.10](https://gitee.com/openharmony/kernel_linux_5.10/blob/master/README)
245
246[kernel\_linux\_6.6](https://gitee.com/openharmony/kernel_linux_6.6/blob/master/README)
247