-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,4 +15,6 @@ sticky: true | |
star: false | ||
footer: | ||
copyright: Copyright with marking the source | ||
article: false | ||
index: false | ||
--- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
--- | ||
title: vscode+qemu开发Linux内核 | ||
|
||
# cover: /assets/images/cover1.jpg | ||
|
||
icon: page | ||
order: 1 | ||
author: ChiChen | ||
date: 2023-10-28 | ||
category: | ||
- 笔记 | ||
- Linux 内核 | ||
tag: | ||
|
||
- Linux 内核 | ||
- 开发环境 | ||
sticky: true | ||
star: true | ||
footer: | ||
copyright: 转载请注明出处 | ||
|
||
--- | ||
|
||
参考于[QEMU调试Linux内核环境搭建](http://kerneltravel.net/blog/2021/debug_kernel_szp/) | ||
|
||
## 获取Linux源码 | ||
|
||
- 略 | ||
|
||
## 编译Linux内核 | ||
|
||
```bash | ||
cd /linux | ||
export ARCH=x86 | ||
make CC=clang x86_64_defconfig | ||
``` | ||
|
||
### config可选项 | ||
|
||
```bash | ||
|
||
# 可选,可以开启调试选项 | ||
make menuconfig | ||
# in menu | ||
Kernel hacking ---> | ||
[*] Kernel debugging | ||
Compile-time checks and compiler options ---> | ||
[*] Compile the kernel with debug info | ||
[*] Provide GDB scripts for kernel debuggin | ||
|
||
# 关闭内核随机地址 | ||
Processor type and features ----> | ||
[] Randomize the address of the kernel image (KASLR) | ||
``` | ||
|
||
### 编译 | ||
|
||
```bash | ||
#编译,并用来获取 compile_commands.json 文件 | ||
bear make CC=clang -j 32 | ||
# 结束输出Kernel: arch/x86/boot/bzImage is ready | ||
``` | ||
|
||
### clangd配置 | ||
|
||
```bash | ||
--compile-commands-dir=${workspaceFolder} | ||
--background-index | ||
--completion-style=detailed | ||
--header-insertion=never | ||
-log=info | ||
-pretty | ||
|
||
``` | ||
|
||
## 配置BusyBox | ||
|
||
启动内核还需要一个具有根文件系统的磁盘镜像文件,根文件系统中提供可供交互的shell程序以及一些常用工具命令。 | ||
我们借助busybox工具来制作根文件系统。 | ||
|
||
```bash | ||
# 下载busybox源码 | ||
wget https://busybox.net/downloads/busybox-1.32.1.tar.bz2 | ||
tar -xvf busybox-1.32.1.tar.bz2 | ||
cd busybox-1.32.1 | ||
make menuconfig | ||
# 配置为静态编译 | ||
# Settings ---> | ||
# [*] Build BusyBox as a static binary (no shared libs) | ||
``` | ||
|
||
## 制作 rootfs | ||
|
||
```bash | ||
szp@r420-PowerEdge-R420:~/busybox-1.32.0$ dd if=/dev/zero of=rootfs.img bs=1M count=10 | ||
|
||
szp@r420-PowerEdge-R420:~/busybox-1.32.0$ mkfs.ext4 rootfs.img | ||
|
||
szp@r420-PowerEdge-R420:~/busybox-1.32.0$ mkdir fs | ||
|
||
szp@r420-PowerEdge-R420:~/busybox-1.32.0$ sudo mount -t ext4 -o loop rootfs.img ./fs | ||
|
||
szp@r420-PowerEdge-R420:~/busybox-1.32.0$ sudo make install CONFIG_PREFIX=./fs | ||
|
||
szp@r420-PowerEdge-R420:~/busybox-1.32.0/fs$ sudo mkdir proc dev etc home mnt | ||
|
||
szp@r420-PowerEdge-R420:~/busybox-1.32.0/fs$ sudo cp -r ../examples/bootfloppy/etc/* etc/ | ||
|
||
szp@r420-PowerEdge-R420:~/busybox-1.32.0$ sudo chmod -R 777 fs/ | ||
|
||
# 写在 rootfs.img | ||
szp@r420-PowerEdge-R420:~/busybox-1.32.0$ sudo umount fs | ||
``` | ||
|
||
## 启动qemu | ||
|
||
这里用的是我的配置,就是要有这个目录结构,然后cd进linux源码文件夹执行下面的命令即可 | ||
|
||
```bash | ||
/. | ||
/.. | ||
/linux | ||
/busybox-1.32.1 | ||
``` | ||
|
||
```bash | ||
qemu-system-x86_64 -kernel ./arch/x86_64/boot/bzImage -hda ../busybox-1.32.1/rootfs.img -append "root=/dev/sda console=ttyS0" -nographic | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ sticky: true | |
star: false | ||
footer: | ||
copyright: 转载请注明出处 | ||
isOriginal: true | ||
--- | ||
|
||
## 前言 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ sticky: true | |
star: false | ||
footer: | ||
copyright: 转载请注明出处 | ||
isOriginal: true | ||
--- | ||
|
||
## Chapter1 编译器组成 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ sticky: true | |
star: false | ||
footer: | ||
copyright: 转载请注明出处 | ||
isOriginal: true | ||
--- | ||
|
||
# Chapter2 词法分析 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ sticky: true | |
star: false | ||
footer: | ||
copyright: 转载请注明出处 | ||
isOriginal: true | ||
--- | ||
|
||
# Chapter3 语法分析 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ sticky: true | |
star: false | ||
footer: | ||
copyright: 转载请注明出处 | ||
isOriginal: true | ||
--- | ||
|
||
# Chapter4 语义分析 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ sticky: true | |
star: false | ||
footer: | ||
copyright: 转载请注明出处 | ||
isOriginal: true | ||
--- | ||
|
||
# Chapter5 代码生成 | ||
|