ccccourse / sp109b

系統程式 -- 109 下學期 (陳鍾誠課程的學生習題與報告)
0 stars 38 forks source link

2021/6/16 系統程式 簽到及發問區! #9

Open ccckmit opened 3 years ago

ccckmit commented 3 years ago

請寫『姓名:到』!

yichien1019 commented 3 years ago

110810519 劉怡謙 到

tenkai0812 commented 3 years ago

110810510 連思凱 到

FUYUHSUAN commented 3 years ago

110810505 傅于軒 到

nohano1l commented 3 years ago

張佑豪 :到

laiy790 commented 3 years ago

110810523 陳艾葳 到

Deng-James commented 3 years ago

110810549 鄧筌祐 到

chenimim commented 3 years ago

110810526 陳霈君

FUYUHSUAN commented 3 years ago

直播好像有點卡卡

Pengbowei commented 3 years ago

110810531 彭柏瑋 到

AIONLin commented 3 years ago

110810521 林妍汝

Chen-Chun commented 3 years ago

110810501 陳淳

ZYZatNQU commented 3 years ago

110610147 周育仲 到

07Nick-kciN21 commented 3 years ago

110810518徐仁鴻到

JANZC2020 commented 3 years ago

110610557 詹子慶 到

WWW-Jack commented 3 years ago

110810503 張凱証 到

110810550 commented 3 years ago

110810550 呂學誠 到

www-abcdefg commented 3 years ago

110810534 柯泓吉 到

ukarara commented 3 years ago

110710516 蘇祐汝 到

kalen2019 commented 3 years ago

110610541 蔡文宏 到

ccckmit commented 3 years ago

https://gitlab.com/ccc109/sp/-/tree/master/10-riscv

hung890202 commented 3 years ago

110810533 洪嘉駿 到

Chen-Chun commented 3 years ago

老師直播一直卡卡

lzc2021 commented 3 years ago

110710524 林子淳 到

ccckmit commented 3 years ago

老師直播一直卡卡

如果太卡就直播完再看

NubletZ commented 3 years ago

110810538 魏美亞 到

ccckmit commented 3 years ago

安裝

ccckmit commented 3 years ago

https://gitlab.com/ccc109/sp/-/blob/master/10-riscv/01-riscv/RISC-V%E8%99%95%E7%90%86%E5%99%A8.md

st741963456 commented 3 years ago

110810551 許嘉烜 到

ccckmit commented 3 years ago

https://gitlab.com/ccc109/sp/-/tree/master/10-riscv/02-sp/01-install

ccckmit commented 3 years ago

開放給學生用的 linode 的帳號密碼

mac020:ccc mac020$ ssh guest@programmermedia.org
guest@programmermedia.org's password: sp109c

./run.sh

請試試 apt-get install qemu

ccckmit commented 3 years ago

用 gcc 編譯 C 語言程式

guest@localhost:~/sp/10-riscv/02-sp/02-gcc$ riscv64-unknown-elf-gcc -S add.c -o add.s
guest@localhost:~/sp/10-riscv/02-sp/02-gcc$ ls
add32.elf  add.c    add.s     RISC-V上gcc工具鏈的使用.md
add64.elf  add.elf  build.sh  run.md
guest@localhost:~/sp/10-riscv/02-sp/02-gcc$ cat add.c
int add(int a, int b) {
  return a+b;
}
guest@localhost:~/sp/10-riscv/02-sp/02-gcc$ cat add.s
        .file   "add.c"
        .option nopic
        .attribute arch, "rv64i2p0_m2p0_a2p0_f2p0_d2p0_c2p0"
        .attribute unaligned_access, 0
        .attribute stack_align, 16
        .text
        .align  1
        .globl  add
        .type   add, @function
add:
        addi    sp,sp,-32
        sd      s0,24(sp)
        addi    s0,sp,32
        mv      a5,a0
        mv      a4,a1
        sw      a5,-20(s0)
        mv      a5,a4
        sw      a5,-24(s0)
        lw      a4,-20(s0)
        lw      a5,-24(s0)
        addw    a5,a4,a5
        sext.w  a5,a5
        mv      a0,a5
        ld      s0,24(sp)
        addi    sp,sp,32
        jr      ra
        .size   add, .-add
        .ident  "GCC: (GNU) 9.2.0"
ccckmit commented 3 years ago
guest@localhost:~/sp/10-riscv/02-sp/02-gcc$ riscv64-unknown-elf-gcc -c add.s -o add.elf
guest@localhost:~/sp/10-riscv/02-sp/02-gcc$ ls
add32.elf  add.c    add.s     RISC-V上gcc工具鏈的使用.md
add64.elf  add.elf  build.sh  run.md
guest@localhost:~/sp/10-riscv/02-sp/02-gcc$ riscv64-unknown-elf-objdump -d add.elf

add.elf:     file format elf64-littleriscv

Disassembly of section .text:

0000000000000000 <add>:
   0:   1101                    addi    sp,sp,-32
   2:   ec22                    sd      s0,24(sp)
   4:   1000                    addi    s0,sp,32
   6:   87aa                    mv      a5,a0
   8:   872e                    mv      a4,a1
   a:   fef42623                sw      a5,-20(s0)
   e:   87ba                    mv      a5,a4
  10:   fef42423                sw      a5,-24(s0)
  14:   fec42703                lw      a4,-20(s0)
  18:   fe842783                lw      a5,-24(s0)
  1c:   9fb9                    addw    a5,a5,a4
  1e:   2781                    sext.w  a5,a5
  20:   853e                    mv      a0,a5
  22:   6462                    ld      s0,24(sp)
  24:   6105                    addi    sp,sp,32
  26:   8082                    ret

老師,我有問題Ctrl+A+X跳不出去

guest@localhost:~$ cd sp/10-riscv/02-sp/04-hello/virt
guest@localhost:~/sp/10-riscv/02-sp/04-hello/virt$ ls
cfg.inc  hello.elf  hello.ld  README.md  run.sh
guest@localhost:~/sp/10-riscv/02-sp/04-hello/virt$ ./run.sh
Hello.

試試 Ctrl-C 或 Ctrl-A C 再用 quit 指令

ccckmit commented 3 years ago

請連上 linode 先試試看!

開放給學生用的 linode 的帳號密碼

mac020:ccc mac020$ ssh guest@programmermedia.org
guest@programmermedia.org's password: sp109c
mark456tung commented 3 years ago

董長茂 到

ccckmit commented 3 years ago

休息到 2:40

ccckmit commented 3 years ago

Part 2 直播 -- https://www.facebook.com/ccckmit/videos/10159045756766893/?__tn__=%2CO

ccckmit commented 3 years ago

用 gcc 做組譯動作

uest@localhost:~/sp/10-riscv/02-sp/03-asm$ ls
add.elf  add.s  assembly.md  Makefile  README.md
guest@localhost:~/sp/10-riscv/02-sp/03-asm$ cat add.s
main:
  addi x29, x0, 5
  addi x30, x0, 37
  add x31, x30, x29
guest@localhost:~/sp/10-riscv/02-sp/03-asm$ make
riscv64-unknown-elf-gcc -Wl,-Ttext=0x0 -nostdlib -o add.elf add.s
/usr/local/lib/gcc/riscv64-unknown-elf/9.2.0/../../../../riscv64-unknown-elf/bin/ld: warning: cannot find entry symbol _start; defaulting to 0000000000000000
riscv64-unknown-elf-objcopy -O binary add.elf add.bin
riscv64-unknown-elf-objdump -d add.elf

add.elf:     file format elf64-littleriscv

Disassembly of section .text:

0000000000000000 <main>:
   0:   00500e93                li      t4,5
   4:   02500f13                li      t5,37
   8:   01df0fb3                add     t6,t5,t4
ccckmit commented 3 years ago

gcc 編譯後用 qemu 執行

guest@localhost:~/sp/10-riscv/02-sp$ cd 04-hello/
guest@localhost:~/sp/10-riscv/02-sp/04-hello$ ls
hello.s  README0.md  README.md  sifive_e  sifive_u  virt
guest@localhost:~/sp/10-riscv/02-sp/04-hello$ cat hello.s
.align 2
.include "cfg.inc"
.equ UART_REG_TXFIFO,   0

.section .text
.globl _start

_start:
        csrr  t0, mhartid             # read hardware thread id (`hart` stands for `hardware thread`)
        bnez  t0, halt                # run only on the first hardware thread (hartid == 0), halt all the other threads

        la    sp, stack_top           # setup stack pointer

        la    a0, msg                 # load address of `msg` to a0 argument register
        jal   puts                    # jump to `puts` subroutine, return address is stored in ra regster

halt:   j     halt                    # enter the infinite loop

puts:                                 # `puts` subroutine writes null-terminated string to UART (serial communication port)
                                      # input: a0 register specifies the starting address of a null-terminated string
                                      # clobbers: t0, t1, t2 temporary registers

        li    t0, UART_BASE           # t0 = UART_BASE
1:      lbu   t1, (a0)                # t1 = load unsigned byte from memory address specified by a0 register
        beqz  t1, 3f                  # break the loop, if loaded byte was null

                                      # wait until UART is ready
2:      lw    t2, UART_REG_TXFIFO(t0) # t2 = uart[UART_REG_TXFIFO]
        bltz  t2, 2b                  # t2 becomes positive once UART is ready for transmission
        sw    t1, UART_REG_TXFIFO(t0) # send byte, uart[UART_REG_TXFIFO] = t1

        addi  a0, a0, 1               # increment a0 address by 1 byte
        j     1b

3:      ret

.section .rodata
msg:
     .string "Hello.\n"
guest@localhost:~/sp/10-riscv/02-sp/04-hello$ cd virt
guest@localhost:~/sp/10-riscv/02-sp/04-hello/virt$ ./run.sh
Hello.
QEMU: Terminated // 要按 Ctrl A-X 才能離開
guest@localhost:~/sp/10-riscv/02-sp/04-hello/virt$ cd ..
guest@localhost:~/sp/10-riscv/02-sp/04-hello$ cd sifive-e
-bash: cd: sifive-e: No such file or directory
guest@localhost:~/sp/10-riscv/02-sp/04-hello$ cd sifive_e
guest@localhost:~/sp/10-riscv/02-sp/04-hello/sifive_e$ ./run.sh
Hello.
QEMU: Terminated // 要按 Ctrl A-X 才能離開
yichien1019 commented 3 years ago

老師,我有問題

user@user-myubuntu:~/sp/10-riscv/02-sp/04-hello/virt$ chmod +x run.sh
user@user-myubuntu:~/sp/10-riscv/02-sp/04-hello/virt$ ./run.sh
./run.sh: 列 5: qemu-system-riscv32:指令找不到
FUYUHSUAN commented 3 years ago

老師,我有問題Ctrl+A+X跳不出去

guest@localhost:~$ cd sp/10-riscv/02-sp/04-hello/virt
guest@localhost:~/sp/10-riscv/02-sp/04-hello/virt$ ls
cfg.inc  hello.elf  hello.ld  README.md  run.sh
guest@localhost:~/sp/10-riscv/02-sp/04-hello/virt$ ./run.sh
Hello.
victor0520 commented 3 years ago

11081532 汪建同 到

ccckmit commented 3 years ago

老師,我有問題Ctrl+A+X跳不出去

guest@localhost:~$ cd sp/10-riscv/02-sp/04-hello/virt
guest@localhost:~/sp/10-riscv/02-sp/04-hello/virt$ ls
cfg.inc  hello.elf  hello.ld  README.md  run.sh
guest@localhost:~/sp/10-riscv/02-sp/04-hello/virt$ ./run.sh
Hello.

試試 Ctrl-C 或 Ctrl-A C 後打 exit

FUYUHSUAN commented 3 years ago

老師,我有問題Ctrl+A+X跳不出去

guest@localhost:~$ cd sp/10-riscv/02-sp/04-hello/virt
guest@localhost:~/sp/10-riscv/02-sp/04-hello/virt$ ls
cfg.inc  hello.elf  hello.ld  README.md  run.sh
guest@localhost:~/sp/10-riscv/02-sp/04-hello/virt$ ./run.sh
Hello.

Ctrl+A+C也不行

ccckmit commented 3 years ago
.align 2
.include "cfg.inc"
.equ UART_REG_TXFIFO,   0

.section .text
.globl _start

_start:
        csrr  t0, mhartid             # read hardware thread id (`hart` stands for `hardware thread`)
        bnez  t0, halt                # run only on the first hardware thread (hartid == 0), halt all the other threads

        la    sp, stack_top           # setup stack pointer

        la    a0, msg                 # load address of `msg` to a0 argument register
        jal   puts                    # jump to `puts` subroutine, return address is stored in ra regster

halt:   j     halt                    # enter the infinite loop

puts:                                 # `puts` subroutine writes null-terminated string to UART (serial communication port)
                                      # input: a0 register specifies the starting address of a null-terminated string
                                      # clobbers: t0, t1, t2 temporary registers

        li    t0, UART_BASE           # t0 = UART_BASE
1:      lbu   t1, (a0)                # t1 = load unsigned byte from memory address specified by a0 register
        beqz  t1, 3f                  # break the loop, if loaded byte was null

                                      # wait until UART is ready
2:      lw    t2, UART_REG_TXFIFO(t0) # t2 = uart[UART_REG_TXFIFO]
        bltz  t2, 2b                  # t2 becomes positive once UART is ready for transmission
        sw    t1, UART_REG_TXFIFO(t0) # send byte, uart[UART_REG_TXFIFO] = t1

        addi  a0, a0, 1               # increment a0 address by 1 byte
        j     1b

3:      ret

.section .rodata
msg:
     .string "Hello.\n"
FUYUHSUAN commented 3 years ago

老師,直播好像不見了

AIONLin commented 3 years ago

斷開

ccckmit commented 3 years ago

老師,直播好像不見了

重新開始了:https://www.facebook.com/ccckmit/videos/10159045827521893/?__tn__=%2CO

brian891005 commented 3 years ago

110810546姜玉尚 到

ccckmit commented 3 years ago

改用 C語言印 Hello

guest@localhost:~/sp/10-riscv/03-mini-riscv-os/01-HelloOs$ make clean
rm -f *.elf
guest@localhost:~/sp/10-riscv/03-mini-riscv-os/01-HelloOs$ make
riscv64-unknown-elf-gcc -nostdlib -fno-builtin -mcmodel=medany -march=rv32ima -mabi=ilp32 -T os.ld -o os.elf start.s os.c
guest@localhost:~/sp/10-riscv/03-mini-riscv-os/01-HelloOs$ make qemu
Press Ctrl-A and then X to exit QEMU
qemu-system-riscv32 -nographic -smp 4 -machine virt -bios none -kernel os.elf
Hello OS!
QEMU: Terminated
ccckmit commented 3 years ago

os.c

#include <stdint.h>

#define UART        0x10000000            // RISC-V 的 virt 虛擬機,其 UART 映射區起始位址為 0x10000000
#define UART_THR    (uint8_t*)(UART+0x00) // THR:transmitter holding register 傳送暫存器
#define UART_LSR    (uint8_t*)(UART+0x05) // LSR:line status register 輸出狀態暫存器
#define UART_LSR_EMPTY_MASK 0x40          // LSR Bit 6: 當 LSR 的第六位元為 1 時,代表傳送區為空的,可以傳了 (Transmitter empty; both the THR and LSR are empty)

void lib_putc(char ch) { // 透過 UART 傳送字元 ch 給宿主機印出
    while ((*UART_LSR & UART_LSR_EMPTY_MASK) == 0); // 一直等到 UART LSR 傳送區為空,可以傳送了 
    *UART_THR = ch; // 將字元 ch 發送出去
}

void lib_puts(char *s) { // 印出字串到宿主機。
    while (*s) lib_putc(*s++);
}

int os_main(void)
{
    lib_puts("Hello OS!\n");
    while (1) {} // 讓主程式卡在這裡,用無窮迴圈停止!
    return 0;
}