MIT-6.s081-OS lab syscall :user-level threads and alarm

本文详细介绍了RISC-V架构下操作系统实验,涉及user-level threads的创建与切换,以及alarm机制的实现。通过分析代码和调试,理解线程调度的关键点,包括线程状态切换、寄存器保存与恢复。同时,实验中还实现了alarm功能,通过定时中断实现线程间的定时切换,讨论了多对一线程模型和合作式线程调度的原理。

代码:https://github.com/RedemptionC/xv6-riscv-6s081/tree/syscall

warm up

(对riscv汇编的熟悉,其实使用gdb调试汇编是”最好“的学习方法)

回答一下几个问题:

Which registers contain arguments to functions? For example, whichregister holds 13 in main's call to printf?

这个很简单,从call.asm就能看出来,是a2

  printf("%d %d\n", f(8)+1, 13);
  24:	4635                	li	a2,13
  26:	45b1                	li	a1,12
  28:	00000517          	auipc	a0,0x0 # save pc+0 to a0
  2c:	7d050513          	addi	a0,a0,2000 # 7f8 <malloc+0xea>
  30:	00000097          	auipc	ra,0x0 # save pc to ra
  34:	620080e7          	jalr	1568(ra) # 650 <printf>

Where is the function call to f from main? Where is the call to g? (Hint:the compiler may inline functions.)

确实,从汇编代码中可以看出,对f,g的调用被优化掉了,体现在汇编代码里是一个立即数:如上图,地址为26

At what address is the function printf located?

printf的地址,注意这里没有使用call,而是jalr,代表jump and link register,语法是:

jalr dst src imm

作用是跳转到imm(src),并且"set the location of the instruction to dst"(这里好像是指把一个地址复制给dst,一般要调用函数时ra,但是一方面这个代码里不是这么写的,这里是在之前就要用auipc设置了ra,而方面感觉risc-v的汇编形式很灵活,半天没找到特殊说明,所以先就这样吧TODO)

注意此时ra等于0x30(是有上一条auipc设置的,pc+0=0x30,注意当时的pc是0x30,而不是0x34,因为pc指向的是即将要执行的一条指令,在执行auipc之前,那当然就是auipc了),再加上1568,就是0x650

What value is in the register ra just after the jalr to printf in main?

这里是问,jalr之后,ra是多少,本来以为jalr里没有出现ra,所以ra没有变,还是0x30,但是通过debug,发现是0x38,也就是指向了正确的返回地址:jalr的下一条

   0x0000000000000030 <main+20>:        97 00 00 00     auipc   ra,0x0
=> 0x0000000000000034 <main+24>:        e7 80 00 62     jalr    1568(ra) # 0x650 <printf>
=> 0x0000000000000038 <main+28>:        01 45   li      a0,0

因为auipc和jalr两条指令所占字节共为8个(之前pc为0x30,指向的是auipc那一行的开头)

这里debug,用到了

set disassemble-next-line on

display /x $reg(你选定的寄存器)

[2025.10.09-14.25.03:906][ 0]LogDeviceProfileManager: Going up to parent DeviceProfile [] [2025.10.09-14.25.03:906][ 0]LogConfig: Applying CVar settings from Section [Startup] File [../../../Engine/Config/ConsoleVariables.ini] [2025.10.09-14.25.03:906][ 0]LogConfig: Set CVar [[r.DumpShaderDebugInfo:2]] [2025.10.09-14.25.03:906][ 0]LogConfig: Set CVar [[p.chaos.AllowCreatePhysxBodies:1]] [2025.10.09-14.25.03:906][ 0]LogConfig: Set CVar [[fx.SkipVectorVMBackendOptimizations:1]] [2025.10.09-14.25.03:906][ 0]LogConfig: Set CVar [[r.ForceDebugViewModes:1]] [2025.10.09-14.25.03:906][ 0]LogConfig: Applying CVar settings from Section [ConsoleVariables] File [Engine] [2025.10.09-14.25.03:906][ 0]LogConfig: Set CVar [[HoverDrone.DroneSpeedScalar:0.5]] [2025.10.09-14.25.03:906][ 0]LogConfig: Set CVar [[fx.NiagaraGpuLowLatencyTranslucencyEnabled:1]] [2025.10.09-14.25.03:906][ 0]LogConfig: Set CVar [[P.Chaos.SyncKinematicOnGameThread:1]] [2025.10.09-14.25.03:906][ 0]LogConfig: Set CVar [[MovieRenderPipeline.WaveOutput.WriteDelay:3]] [2025.10.09-14.25.03:906][ 0]LogConfig: Set CVar [[p.ClothPhysics:0]] [2025.10.09-14.25.03:906][ 0]LogConfig: Set CVar [[g.TimeoutForBlockOnRenderFence:60000]] [2025.10.09-14.25.03:906][ 0]LogInit: Unix hardware info: [2025.10.09-14.25.03:906][ 0]LogInit: - we are the first instance of this executable [2025.10.09-14.25.03:906][ 0]LogInit: - this process' id (pid) is 168551, parent process' id (ppid) is 168543 [2025.10.09-14.25.03:906][ 0]LogInit: - we are not running under debugger [2025.10.09-14.25.03:907][ 0]LogInit: - machine network name is 'DESKTOP-2PJK7EV' [2025.10.09-14.25.03:907][ 0]LogInit: - user name is 'lxng' (lxng) [2025.10.09-14.25.03:907][ 0]LogInit: - we're logged in locally [2025.10.09-14.25.03:907][ 0]LogInit: - we're running with rendering [2025.10.09-14.25.03:907][ 0]LogInit: - CPU: GenuineIntel '12th Gen Intel(R) Core(TM) i9-12900K' (signature: 0x90672) [2025.10.09-14.25.03:907][ 0]LogInit: - Number of physical cores available for the process: 12 [2025.10.09-14.25.03:907][ 0]LogInit: - Number of logical cores available for the process: 24 [2025.10.09-14.25.03:907][ 0]LogInit: - GPU Brand Info: UnknownVendor [2025.10.09-14.25.03:907][ 0]LogInit: - Memory allocator used: binned2 [2025.10.09-14.25.03:907][ 0]LogInit: - This binary is optimized with LTO: no, PGO: no, instrumented for PGO data collection: no [2025.10.09-14.25.03:907][ 0]LogInit: - This is an internal build. [2025.10.09-14.25.03:907][ 0]LogCore: Benchmarking clocks: [2025.10.09-14.25.03:907][ 0]LogCore: - CLOCK_MONOTONIC (id=1) can sustain 50522507 (50523K, 51M) calls per second without zero deltas. [2025.10.09-14.25.03:907][ 0]LogCore: - CLOCK_MONOTONIC_RAW (id=4) can sustain 54124297 (54124K, 54M) calls per second without zero deltas. [2025.10.09-14.25.03:907][ 0]LogCore: - CLOCK_MONOTONIC_COARSE (id=6) can sustain 255663865 (255664K, 256M) calls per second with 99.999906% zero deltas. [2025.10.09-14.25.03:907][ 0]LogCore: Selected clock_id 4 (CLOCK_MONOTONIC_RAW) since it is the fastest support clock without zero deltas. [2025.10.09-14.25.03:907][ 0]LogInit: Unix-specific commandline switches: [2025.10.09-14.25.03:907][ 0]LogInit: -ansimalloc - use malloc()/free() from libc (useful for tools like valgrind and electric fence) [2025.10.09-14.25.03:907][ 0]LogInit: -jemalloc - use jemalloc for all memory allocation [2025.10.09-14.25.03:907][ 0]LogInit: -binnedmalloc - use binned malloc for all memory allocation [2025.10.09-14.25.03:907][ 0]LogInit: -filemapcachesize=NUMBER - set the size for case-sensitive file mapping cache [2025.10.09-14.25.03:907][ 0]LogInit: -useksm - uses kernel same-page mapping (KSM) for mapped memory (OFF) [2025.10.09-14.25.03:907][ 0]LogInit: -ksmmergeall - marks all mmap'd memory pages suitable for KSM (OFF) [2025.10.09-14.25.03:907][ 0]LogInit: -preloadmodulesymbols - Loads the main module symbols file into memory (OFF) [2025.10.09-14.25.03:907][ 0]LogInit: -sigdfl=SIGNAL - Allows a specific signal to be set to its default handler rather then ignoring the signal [2025.10.09-14.25.03:907][ 0]LogInit: -crashhandlerstacksize - Allows setting crash handler stack sizes (204800) [2025.10.09-14.25.03:907][ 0]LogInit: -noexclusivelockonwrite - disables marking files created by the engine as exclusive locked while the engine has them opened [2025.10.09-14.25.03:907][ 0]LogInit: -httpproxy=ADDRESS:PORT - redirects HTTP requests to a proxy (only supported if compiled with libcurl) [2025.10.09-14.25.03:907][ 0]LogInit: -reuseconn - allow libcurl to reuse HTTP connections (only matters if compiled with libcurl) [2025.10.09-14.25.03:907][ 0]LogInit: -virtmemkb=NUMBER - sets process virtual memory (address space) limit (overrides VirtualMemoryLimitInKB value from .ini) [2025.10.09-14.25.03:907][ 0]LogInit: -allowsyscallfilterfile=PATH_TO_FILE - sets up a system call filter allow list. any invalid syscall in this list *will* cause a crash [2025.10.09-14.25.03:907][ 0]LogInit: - Physical RAM available (not considering process quota): 32 GB (31986 MB, 32753812 KB, 33539903488 bytes) [2025.10.09-14.25.03:907][ 0]LogInit: - VirtualMemoryAllocator pools will grow at scale 1.4 [2025.10.09-14.25.03:907][ 0]LogInit: - MemoryRangeDecommit() will be a no-op (re-run with -vmapoolevict to change) [2025.10.09-14.25.03:907][ 0]LogInit: - PageSize 4096 [2025.10.09-14.25.03:907][ 0]LogInit: - BinnedPageSize 65536 [2025.10.09-14.25.03:907][ 0]LogCsvProfiler: Display: Metadata set : extradevelopmentmemorymb="0" [2025.10.09-14.25.03:911][ 0]LogInit: Physics initialised using underlying interface: Chaos [2025.10.09-14.25.03:911][ 0]LogInit: Using OS detected language (en-US-POSIX). [2025.10.09-14.25.03:911][ 0]LogInit: Using OS detected locale (en-US-POSIX). [2025.10.09-14.25.03:912][ 0]LogTextLocalizationManager: No specific localization for 'en-US-POSIX' exists, so 'en' will be used for the language. [2025.10.09-14.25.03:937][ 0]LogHAL: Warning: Game icon not found. [2025.10.09-14.25.04:238][ 0]LogSlate: New Slate User Created. Platform User Id 0, User Index 0, Is Virtual User: 0 [2025.10.09-14.25.04:238][ 0]LogSlate: Slate User Registered. User Index 0, Is Virtual User: 0
最新发布
10-10
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值