Giter Site home page Giter Site logo

ics-pa-gitbook's Introduction

nju-projectn.github.com/ics-pa-gitbook

Please access here for all the lecture notes.

NOTE: This repo is updated by forced push. Run bash update.sh to keep the repo up-to-date.

ics-pa-gitbook's People

Contributors

sashimi-yzh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

ics-pa-gitbook's Issues

关于讲义 3.2 节没有及时更新的一处路径

在 PA3.2 - 穿越时空的旅程 - 将上下文管理抽象成CTE - 设置异常入口地址中,讲义中描述的是:这最终会调用位于 abstract-machine/am/src/nemu/isa/$ISA/cte.c 中的 cte_init() 函数;但在 ics2021 及以后的版本中,该路径应该为 abstract-machine/am/src/$ISA/nemu/cte.c

麻烦老师啦 ~

Is there a plan to migrate to cmake?

Though the Makefile's work smoothly in this project, I think it might be a good idea to migrate to cmake. cmake is widely used in modern software development, and if the students can learn how to use it in this course project then they will get better prepared for industry.

installing llvm of pa0

安装llvm的命令是不是有些问题:apt-get install llvm # llvm project, which contains libraries used later
我在ubuntu21下面运行上面的命令后安装了llvm-6,在之后的make出现许多的错误
llvm/Support/TargetRegistry.h: No such file or directory

在另一个issue里面告知了可以安装llvm-11来解决,那为什么不直接把上面的apt-get install llvm 替换为apt-get install llvm-11呢?
modify nemu/src/utils/filelist.mk to use llvm-config-11 instead of llvm-config
谢谢。

ics2020 PA2.4 test_memset函数有误

data 初始化的时候是 data[i] = i + 1。所以 check_seq 的时候 val 应为 l + 1,即

void test_memset() {
  int l, r;
  for (l = 0; l < N; l ++) {
    for (r = l + 1; r <= N; r ++) {
      reset();
      uint8_t val = (l + r) / 2;
      memset(data + l, val, r - l);
      check_seq(0, l, 1);
      check_eq(l, r, val);
      check_seq(r, N, r + 1);
    }
  }
}

2019的PA 可以提供PDF Version 么

Generate a PDF file

$ gitbook pdf ./ ./mybook.pdf

Generate an ePub file

$ gitbook epub ./ ./mybook.epub

Generate a Mobi file

$ gitbook mobi ./ ./mybook.mobi

html网页本地跳转有问题

你好,网页本地阅读有两个小瑕疵:
1.本地阅读时,不支持当前标签页跳转,需要<在新标签页中打开链接>;(theme.js可修改)
2.通过点击Introduction超链接无法正确跳转到index.html文件。(data-path="./"和href="./")

4.3 节 RISC-V 架构的 Difftest (Spike) 问题

标准RISC-V的分页机制需要在S模式及U模式下才能开启, 而在M模式下的访存并不会进行MMU的地址转换. 但我们在NEMU中进行了简化, 允许M模式的访存也进行地址转换, 这样可以避免引入S模式相关的细节……

然而在 nemu/tools/spike-diff/repo/riscv/mmu.h 的 decode_vm_info() 函数中,如果目前模式是 M 模式,那么会直接返回全零的 vm_info

https://github.com/NJU-ProjectN/riscv-isa-sim/blob/4740dedcfd58f1b881d570cca0f8b697e17b8a5f/riscv/mmu.h#L496-L499

inline vm_info decode_vm_info(int xlen, bool stage2, reg_t prv, reg_t satp)
{
  if (prv == PRV_M) {
    return {0, 0, 0, 0, 0};

这导致了 M 模式下作为对照的 Spike 不会去做地址转换:

https://github.com/NJU-ProjectN/riscv-isa-sim/blob/4740dedcfd58f1b881d570cca0f8b697e17b8a5f/riscv/mmu.cc#L384-L390

reg_t mmu_t::walk(reg_t addr, access_type type, reg_t mode, bool virt, bool hlvx)
{
  reg_t page_mask = (reg_t(1) << PGSHIFT) - 1;
  reg_t satp = (virt) ? proc->get_state()->vsatp : proc->get_state()->satp;
  vm_info vm = decode_vm_info(proc->max_xlen, false, mode, satp);
  if (vm.levels == 0)
    return s2xlate(addr, addr & ((reg_t(2) << (proc->xlen-1))-1), type, type, virt, hlvx) & ~page_mask; // zero-extend from xlen

从而导致 difftest 出错,但是 gitbook 中似乎没有提到这一点。如果 PA 的 NEMU 不打算去实现 S 模式的话,这里的修改恐怕是必须的,可能可以加一个 macro 来弄。

[特性请求] 左侧目录可通过按钮隐藏

在 1920x1080 分辨率显示器中左右分屏一半,正文无法完全显示,需要手动左右拖动。增加隐藏左侧目录按钮或对正文进行动态排版能够稍微提升下阅读体验。

[Suggestions] PA3 NDL_GetTicks() 函数功能描述有错误/歧义

原文

// 以毫秒为单位返回系统时间
uint32_t NDL_GetTicks();

建议修改成

// 以毫秒为单位返回自 NDL 初始化时经过的时间

如果 NDL_GetTicks() 不从0开始,而是从 gettimeofday() 所指示的时间开始,PAL 在一些时间段运行会遇到莫名其妙的问题,疑似时间溢出了。

Install Ubuntu部分的补充

在该部分结尾有提示“不要使用Ubuntu的Software Updater”。不仅如此,在初次安装时也可能有默认选中的选项会导致自动更新:
image

ics2020 PA4.4 抢占多任务描述不完整

  • 修改raise_intr()中的代码, 让处理器进入关中断状态:
    • x86 - 在保存EFLAGS寄存器后, 将其IF位置为0

后面应该还有一个:修改iret指令的实现, 将 EFLAGS 寄存器的 IF 位置为 1。
不知道是不是你们忘记写了还是你们故意这样设计的,如果是故意设置这样一个小坑当我没说😂😂。

typo error from pa1

在终端输出相应的按键信息的小程序,搜索建议的关键字"SDL keystrobe" 是不是应该是 "SDL keystroke"?

[Suggestions] 在 am_native 中增加允许声卡多次打开的代码,并在 pa3、pa4 中完善声卡相关的提示

在 PA 中出现的所有设备中,声卡应该是最特殊的一种:它在打开时会持续地“记录” freq, channels, samples 三个参数,且在之后不会更改。当机器上自始至终仅运行一个程序时,这不会出现问题。但是在 pa3 中,若 /bin/nterm 存在音乐并进入其它程序(如 pal),或若在程序 exit 后重新进入 /bin/nterm 并打开新的程序。在这种情况下,声卡会被打开多次。若两次参数 freq 等存在差异,则可能会导致音频播放出现异常。故建议在 am_native 上添加允许声卡多次打开的代码,并在讲义中加入在 nemu 的声卡部分进行调整,允许其多次打开的提示。以下是我的修改:

--- a/abstract-machine/am/src/native/ioe/audio.c
+++ b/abstract-machine/am/src/native/ioe/audio.c
@@ -6,6 +6,7 @@
 
 static int rfd = -1, wfd = -1;
 static volatile int count = 0;
+static int audio_on;
 
 void __am_audio_init() {
   int fds[2];
@@ -41,6 +42,13 @@ static void audio_write(uint8_t *buf, int len) {
 }
 
 void __am_audio_ctrl(AM_AUDIO_CTRL_T *ctrl) {
+  if (audio_on) {
+    SDL_CloseAudio();
+
+    // clean the pipe
+    static uint8_t buf[4096];
+    while (read(rfd, buf, 4096) > 0) ;
+  }
   SDL_AudioSpec s = {};
   s.freq = ctrl->freq;
   s.format = AUDIO_S16SYS;
@@ -52,6 +60,7 @@ void __am_audio_ctrl(AM_AUDIO_CTRL_T *ctrl) {
   count = 0;
   int ret = SDL_InitSubSystem(SDL_INIT_AUDIO);
   if (ret == 0) {
+    audio_on = 1;
     SDL_OpenAudio(&s, NULL);
     SDL_PauseAudio(0);
   }

其中管道的清空是必要的,否则可能出现阻塞。但我这里使用的方案个人认为并不优雅,不知道有没有更好的方法。

在 pa4 中,涉及到多个程序的切换(主要是最后的 F1, F2, F3 演示。让两个程序分时运行同时使用声卡应是不允许的,正如不能让两个程序朴素地同时使用 vga)。此时个人认为应该由 OS 进行声卡的切换管理,比如可以在 pcb 中加入相关参数。同样建议在 pa4 讲义中加入相关提示。

Suggestions:

  1. (important) Let the am native support the reopen of sound card.
  2. (not quite important) Add prompt in the teaching materials, as mentioned above. I think this is not quite important because the cause of this problem is not obscure and those who are willing to complete the audio part are strong enough to solve this problem on their own.

在 Ubuntu 20.04 x86 平台上进行 riscv32 交叉编译遇到问题

在 Ubuntu 20.04 x86 平台上安装 riscv32 交叉编译工具后尝试编译,产生报错

# Building dummy-run [riscv32-nemu]
+ CC tests/dummy.c
cc1: error: ‘-march=rv32im_zicsr’: unsupported ISA subset ‘z’
make[1]: *** [/home/konpoku/dev/NJU-ics2023-pa/abstract-machine/Makefile:110: /home/konpoku/dev/NJU-ics2023-pa/am-kernels/tests/cpu-tests/build/riscv32-nemu/tests/dummy.o] Error 1
test list [1 item(s)]: dummy
[         dummy] ***FAIL***

A small advice on PA2 basic infrastructure DiffTest part's snippet

It makes me feel confused when reading the snippet below at first. In the first line of the snippet, there is a dest which is not mentioned in the definition of difftest_memcpy.

// 在DUT host memory的`buf`和REF guest memory的`dest`之间拷贝`n`字节,
// `direction`指定拷贝的方向, `DIFFTEST_TO_DUT`表示往DUT拷贝, `DIFFTEST_TO_REF`表示往REF拷贝
void difftest_memcpy(paddr_t addr, void *buf, size_t n, bool direction);
// `direction`为`DIFFTEST_TO_DUT`时, 获取REF的寄存器状态到`dut`;
// `direction`为`DIFFTEST_TO_REF`时, 设置REF的寄存器状态为`dut`;
void difftest_regcpy(void *dut, bool direction);
// 让REF执行`n`条指令
void difftest_exec(uint64_t n);
// 初始化REF的DiffTest功能
void difftest_init();

I search the source code and find it should be the dest in the definition of sim_t::diff_memcpy which is predefined in spike-diff. (I am using risc-v64, and I admit I did spend time searching just to confirm the dest refers to addr in the context XD )

It is harmless, and clever students may just guess the dest is addr. But I thought it would be better to replace dest with addr since it could make more sense.

PA is such a marvelous job. I hope it will get much more better!

《PC的足迹》已无法阅读

PA结束语中交互时代一节,《PC的足迹》系列由于版权原因现已成为付费文章,需要开通CSDN会员才能阅读。

是否考虑提供第三方链接或者撤回该条目?

文档改进:添加对龙芯架构实验的相关文档

PA实验文档中缺少对loongarch的支持,因此撰写补充文档:

龙芯手册(index.html)

交叉编译链安装(2.2.html#运行第一个c程序)

loongarch32r(手动安装到系统中)

  1. 下载已经编译好的龙芯工具链[loongson-gnu-toolchain](https://gitee.com/loongson-edu/la32r-toolchains/releases/download/v0.0.3/loongson-gnu-toolchain-8.3-x86_64-loongarch32r-linux-gnusf-v2.0.tar.xz)

  2. 解压后添加到系统路径中

#1.解压文件(请将文件名更换为你下载的文件名)
tar -xvf toolchain.tar.xz
#假设解压后的路径为~/Downloads/toolchain(请更换为你的解压后路径)
echo "export PATH=$PATH:~/Downloads/toolchain" >> ~/.bashrc
#使用其他shell请根据自己的shell完成环境变量的修改
source ~/.bashrc
  1. 检验工具链是否成功完成添加
loongarch32r-linux-gnusf-gcc -v

成功添加后得到的结果如下

Using built-in specs.
COLLECT_GCC=loongarch32r-linux-gnusf-gcc
COLLECT_LTO_WRAPPER=/home/public/ics2023/toolchains/loongson-gnu-toolchain-8.3-x86_64-loongarch32r-linux-gnusf-v2.0/bin/../libexec/gcc/loongarch32r-linux-gnusf/8.3.0/lto-wrapper
Target: loongarch32r-linux-gnusf
Configured with: /dev/shm/build_loongarch32r-linux-gnusf_x86_64_v2.0_20230903/src/target_gcc/configure --build=x86_64-redhat-linux --host=x86_64-linux-gnu --target=loongarch32r-linux-gnusf --prefix=/dev/shm/build_loongarch32r-linux-gnusf_x86_64_v2.0_20230903/build-canadian/root/usr --with-sysroot=/dev/shm/build_loongarch32r-linux-gnusf_x86_64_v2.0_20230903/build-canadian/root/usr/loongarch32r-linux-gnusf/sysroot --with-native-system-header-dir=/usr/include --with-gxx-include-dir=/dev/shm/build_loongarch32r-linux-gnusf_x86_64_v2.0_20230903/build-canadian/root/usr/loongarch32r-linux-gnusf/sysroot/usr/include/c++ --enable-languages=c,c++,fortran --with-build-sysroot= --disable-libstdcxx --disable-libgfortran --disable-libgomp --enable-libcc1 --enable-threads=posix --enable-__cxa_atexit --enable-initfini-array --enable-nls --disable-libmpx --disable-bootstrap --with-abi=ilp32s --enable-checking=release --with-pkgversion='LoongArch GNU toolchain LA32 v2.0 (20230903)'
Thread model: posix
gcc version 8.3.0 (LoongArch GNU toolchain LA32 v2.0 (20230903))

difftest安装(2.4.html#differential-testing)

loongarch32r:[QEMU](http://www.qemu.org/). QEMU是一个完整的全系统模拟器, 支持多种ISA. 但我们只能通过基于socket的GDB协议与QEMU进行通信来获取其状态, 通信开销较大. 为了运行龙芯的QEMU, 你还需要像安装工具链一样手动的安装它:

  1. 下载已经编译好的龙芯[QEMU-la32](https://gitee.com/loongson-edu/la32r-QEMU/releases/download/v0.0.2/la32r-QEMU-x86_64-ubuntu-22.04.tar)

  2. 解压后添加到系统路径中

#1.解压文件(请将文件名更换为你下载的文件名)
tar -xvf toolchain.tar.xz
#假设解压后的路径为~/Downloads/toolchain(请更换为你的解压后路径)
echo "export PATH=$PATH:~/Downloads/toolchain" >> ~/.bashrc
#使用其他shell请根据自己的shell完成环境变量的修改
source ~/.bashrc
  1. 检验工具链是否成功完成添加
qemu-system-loongarch32 -version

如果出现报错,则补充需要的动态链接库

sudo apt install libcapstone-dev
sudo apt install libvdeplug-dev

成功安装的输出如下

QEMU emulator version 6.2.50
Copyright (c) 2003-2022 Fabrice Bellard and the QEMU Project developers
  1. 如果你在2024/5/15 之前获取nemu的代码, 请获取新版代码:
cd nemu
git pull origin master

或者手动修改代码:

@@ -29,6 +29,9 @@
29 29   #elif defined(CONFIG_ISA_x86)
30 30   #define ISA_QEMU_BIN "qemu-system-i386"
31 31   #define ISA_QEMU_ARGS
   32 + #elif defined(CONFIG_ISA_loongarch32r)
   33 + #define ISA_QEMU_BIN "qemu-system-loongarch32"
   34 + #define ISA_QEMU_ARGS "-M","ls3a5k32",
32 35   #else
33 36   #error Unsupport ISA
34 37   #endif
@@ -49,6 +52,9 @@ union isa_gdb_regs {
49 52      uint32_t eax, ecx, edx, ebx, esp, ebp, esi, edi;
50 53      uint32_t eip, eflags;
51 54      uint32_t cs, ss, ds, es, fs, gs;
   55  + #elif defined(CONFIG_ISA_loongarch32r)
   56  + uint32_t gpr[32];
   57  + uint32_t pc;
52 58.   #endif
53 59    };
54 60    struct {

异常处理相关(3.2.html中添加)

loongarch32

loongarch32提供syscall指令作为自陷指令,并提供一个EENTRY寄存器来存放入口地址,为了保存程序当前的状态,loongarch32提供一些特殊的系统寄存器,叫控制状态寄存器,在PA中,我们只使用如下如下4个CSR寄存器:

  • CRMD寄存器 - 存放当前模式信息

  • PRMD寄存器 - 存放异常前模式信息

  • ESTAT寄存器 - 存放处理器的状态

  • ERA寄存器 - 异常的返回地址

loongarch32触发异常后硬件的响应过程如下:

  1. 将 CRMD 的 PLV、IE 分别存到 PRMD 的 PPLV、PIE 中,然后将CRMD的PLV置为 0,IE 置为 0;

  2. 将当前 PC 值记录到 CSR.ERA 中

  3. 跳转到异常入口地址

loongarch32通过retn指令从异常处理过程中返回,它将将 PRMD 中的 PPLV、PIE 值恢复到 CRMD 的 PLV、IE 中,并根据ERA寄存器恢复PC

前辈您好,我想咨询下PA3最后的仙剑奇侠转的数据文件怎么获取?

前辈您好:
我在做PA实验第三部分时,最后需要仙剑奇侠传的数据文件进行测试。那个实验的指引说 从课程网站上下载仙剑奇侠传的数据文件, 并放到navy-apps/fsimg/share/games/pal/目录下。
我不是南京大学的学生,这个课程网站要在哪里获取哈。
十分感谢前辈有时间阅读我的提问,谢谢。

关于内核栈和用户栈的分析描述

在4.4中,手册分析了为什么目前不支持并发执行多个用户进程?。然而我对其中的描述有些疑问。
手册中的大意如下:

  1. 假设从用户进程A切换到用户进程B
  2. 操作系统决定要调度用户进程B
  3. CTE通过__am_switch()切换到B的虚拟地址空间
  4. 当__am_switch()切换到B的虚拟地址空间之后,由于A的用户栈并不在B的虚拟地址空间里面,因此代码就不能再访问A的用户栈了

当从A进程切换到B进程时,Context* __am_irq_handle(Context *c)应该返回B进程的上下文地址vaddr_b,然而,这个虚拟地址是对于B进程的地址空间而言的。此时,系统仍运行在A进程的地址空间中;若__am_switch(Context *c)希望访问上下文地址vaddr_b,相当于拿着用户进程A的页表将vaddr_b翻译为某个A进程“拥有”的物理地址。
因此我认为此时系统无法读取B进程用户栈上的上下文,也无法读取其中c->pdir的内容,无法切换到B进程的地址空间。

所以我想这段分析是否应该修改为下面的意思:

为了方便描述, 我们假设某一时刻, 操作系统将要从用户进程A切换到用户进程B. 具体地, 在A运行的时候, 栈指针sp会指向A的用户栈. 当A执行系统调用, 或者中断到来的时候, 就会经过CTE陷入操作系统. 注意到这时CTE是在A的用户栈上保存A的上下文, 而sp也仍然指向A的用户栈. 这时, 操作系统决定要调度用户进程B, 它就会把B的上下文返回给CTE, CTE会依次进行如下操作:

  • 通过__am_switch()切换到B的虚拟地址空间
  • 在trap.S中恢复B的上下文

现在问题来了: **此时系统还运行在A的地址空间中,而B的用户栈并不在A的虚拟地址空间里面! **这意味着, 当__am_switch()收到B的上下文地址后,代码无法访问到其对应的正确物理地址. 于是__am_switch()无法正确地访问B的上下文,也无法从中得知B用户进程的地址空间描述符并切换到B的地址空间

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.