当前位置:网站首页>Debug and run the first xv6 program
Debug and run the first xv6 program
2022-07-06 17:39:00 【Wzzzzzzx】
The original text can be found in Debug and run the first one Xv6 Program Check out , Subsequent updates will be updated to Personal blog
Xv6 When it was just started, it was Machine Mode, After completing the basic configuration, you will jump to Supervisor Mode, In this mode ,main Will complete the configuration of the system as soon as possible and create the first user program .
Pre knowledge
Xv6 Is an operating system that supports multiple processes , stay Makefile Can be seen in CPUS Will be set to... By default 3, For debugging convenience , At startup , You can set it to 1. This will only be in one CPU Run the program on , A single breakpoint can prevent the operation of the entire operating system .
Environmental preparation
The system can use Ubuntu Server 20.04, For debugging GDB yes gdb-multiarch 9.2.0,qemu Then SiFive Provided by the qemu-system-riscv64 5.1.0.
Began to run
Press {% post_link 6s081/ debugging xv6 “ debugging xv6” %} One article to operate , The development and debugging environment can run smoothly . At this time, execute at a terminal make CPUS=1 qemu-gdb Run the system , Then execute... At another terminal gdb-multiarch take gdb Run up , here gdb Will automatically connect to qemu In the debugging environment , If everything goes well , You can see the following output :
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
The target architecture is assumed to be riscv:rv64
warning: No executable has been specified and target does not support
determining executable automatically. Try using the "file" command.
0x0000000000001000 in ?? ()
Starting operation
At the beginning , Can pass b _entry Set a breakpoint at the entrance of the system , stay {% post_link 6s081/ Kernel link script parsing “ Link control script ” %} Mentioned in , The entry point of the system will be set to 0x80000000. But I don't know why , It's on my machine , The address of this breakpoint will be set to 0x80000004, What the teacher showed in class was 0x8000000a. But at this time, if you pass info address _entry see _entry The address of , You can also see its location 0x80000000 On . The specific reasons need to be further explored .
After the breakpoint is set , You can enter c Let the system run , At this time, it will be directly stuck at the breakpoint . For the convenience of debugging , You can use it layout split take gdb The window is divided into instruction and code windows , It is convenient to view the instructions corresponding to each code .
At this time, the system is in Machine Mode, At this time, the environment will be configured , Create a program stack , In order to transfer in Supervisor Mode function C Code . These codes can be ignored for the time being .
Get into main function
Skip over Machine Mode After the relevant code , You can go directly to the beginning of the kernel , This is the same as the ordinary C Same procedure , It's all about main As an entry function . Through the command b main Set the breakpoint to main At function . And then directly input c Let the program run , until main The function stops again .
stay main Various configurations of the system will be completed in the function , Including setting up virtual memory , A page table , File systems, etc , Please refer to Code The comments in . The teacher mentioned in class , These initialization operations are sequential , You can't change it at will .
In these initialized functions , Now we need to pay attention to userinit(). In this function , Will complete the first User Mode The creation of the program .
Get into userinit() function
stay main Function n The command makes the program execute line by line , until userinit() When , change to the use of sth. s Let the program enter the function body .
The operation of this function is to manually create a user process , Set this process to RUNNABLE state , In order to cpu Scheduling execution . Here's an interesting place ,userinit() How to create user processes , It is created directly using the binary form of the program .
The binary code definition of this user program is directly hard coded in initcode In this array , The corresponding executable program is user/initcode.
initcode Program
initcode In the definition of user/initcode.S. The program is Xv6 The first user mode program created , from userinit() It can be seen that , The first user mode program can only be created manually , So it will be required to be sufficient Simple . therefore ,initcode More like a Wedge , Used to lead to the real user program with logic .
As you can see from the code ,initcode Is the use exec This system call is used to create a more complex executable program . It will be init This program is loaded into a0, This is the program name of the program to be created , Then set the parameter argv Load into a1.exec The call number of this system call is 7, So we need to 7 Load into a7 On . After the relevant operations are completed , Will call ecall Give control back to the operating system .
Trigger syscall
As can be seen from the above ,userinit Will create a simple user mode program , The program will use system calls exec To create a truly logical , More complex user mode programs to replace themselves .
The specific details of how the system creates the first program need not be paid too much attention at this time , What we need to pay attention to is the specific logic of system call , So it can be used b syscall Set the breakpoint to the trigger function of the system call . stay userinit() Use directly inside c Let the program run directly to the breakpoint .
syscall This function is located in kernel/syscall.c Inside , The kernel can be p->trapframe->a7 Get the system call number , Use at this time p num You can see that the system call number is loaded into a7 Of 7. Program run to p->trapframe->a0 = syscalls[num](); when , Use it directly s Fall into exec Within this system call .
Get into exec
exec be located kernel/sysfile.c in , After entering this function , The program path to be called will be placed in path, Start parameters are placed in argv in . And then directly use exec take path Created by the program in
The creation is complete
Through the above process , It can correctly create the first user mode program in the true sense ,init. The source code of this program is in user/init.c. You can see from the code that , The only purpose of this program is to modify the file descriptor , take 0 and 1 Force setting to standard I / O . And maintain sh Operation of .
thus , The whole system runs smoothly .
边栏推荐
- MySQL advanced (index, view, stored procedure, function, password modification)
- 全网最全tcpdump和Wireshark抓包实践
- C # nanoframework lighting and key esp32
- C version selenium operation chrome full screen mode display (F11)
- Chrome prompts the solution of "your company management" (the startup page is bound to the company's official website and cannot be modified)
- 学 SQL 必须了解的 10 个高级概念
- Virtual machine startup prompt probing EDD (edd=off to disable) error
- PostgreSQL 14.2, 13.6, 12.10, 11.15 and 10.20 releases
- 复盘网鼎杯Re-Signal Writeup
- 信息与网络安全期末复习(完整版)
猜你喜欢

虚拟机启动提示Probing EDD (edd=off to disable)错误

CTF逆向入门题——掷骰子

2021-03-22 "display login screen during recovery" can't be canceled. The appearance of lock screen interface leads to the solution that the remotely connected virtual machine can't work normally

全网最全tcpdump和Wireshark抓包实践

Wu Jun's trilogy experience (VII) the essence of Commerce

信息与网络安全期末复习(完整版)

轻量级计划服务工具研发与实践

C # nanoframework lighting and key esp32

About selenium starting Chrome browser flash back

Grafana 9 正式发布,更易用,更酷炫了!
随机推荐
MySQL advanced (index, view, stored procedure, function, password modification)
【逆向中级】跃跃欲试
Flexible report v1.0 (simple version)
The art of Engineering (1): try to package things that do not need to be exposed
Wordcloud colormap color set and custom colors
Quick start of Hongmeng system
集成开发管理平台
Garbage first of JVM garbage collector
信息与网络安全期末复习(完整版)
自动化运维利器-Ansible-Playbook
Start job: operation returned an invalid status code 'badrequst' or 'forbidden‘
connection reset by peer
【MySQL入门】第三话 · MySQL中常见的数据类型
The art of Engineering (2): the transformation from general type to specific type needs to be tested for legitimacy
Huawei certified cloud computing hica
应用服务配置器(定时,数据库备份,文件备份,异地备份)
JUnit unit test
Solrcloud related commands
Pyspark operator processing spatial data full parsing (5): how to use spatial operation interface in pyspark
Case: check the empty field [annotation + reflection + custom exception]