Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

riscv-qemuでUARTに文字出力する #3

Closed
tomoyuki-nakabayashi opened this issue Nov 2, 2018 · 16 comments
Closed

riscv-qemuでUARTに文字出力する #3

tomoyuki-nakabayashi opened this issue Nov 2, 2018 · 16 comments

Comments

@tomoyuki-nakabayashi
Copy link
Owner

tomoyuki-nakabayashi commented Nov 2, 2018

ターゲットは、virtでもいいかな。追々考えよう。

https://github.com/riscv/riscv-qemu/wiki

@tomoyuki-nakabayashi
Copy link
Owner Author

https://github.com/michaeljclark/riscv-probe

そうか、ベアメタルサンプルとか言って、elfロードしてるのか。まぁ最初の一歩としてはありか?

@tomoyuki-nakabayashi
Copy link
Owner Author

http://msyksphinz.hatenablog.com/entry/2018/05/07/040000

$ wget https://download.qemu.org/qemu-3.0.0.tar.xz
$ tar xf qemu-3.0.0.tar.xz
$ cd qemu-3.0.0/
$ mkdir build
$ cd build/
$ ../configure --target-list=riscv32-softmmu --prefix=/opt/riscv-qemu
$ make

@tomoyuki-nakabayashi
Copy link
Owner Author

これで、仮想hard driveを読み込むかな。

$ qemu-system-x86_64 -hda bootimage.bin -d int -s

@tomoyuki-nakabayashi
Copy link
Owner Author

https://www.slideshare.net/ssuser479fa3/riscv-berkeley-boot-loader-proxy-kernel

berkeley boot loader、これ読めば少しはわかるかな?

@tomoyuki-nakabayashi
Copy link
Owner Author

tomoyuki-nakabayashi commented Nov 2, 2018

@tomoyuki-nakabayashi
Copy link
Owner Author

tomoyuki-nakabayashi commented Nov 2, 2018

https://github.com/riscv/riscv-qemu/blob/riscv-all/hw/riscv/virt.c#L46-L55

あー、virtだとROMが0x1000に0x11000 bytes (68KB) あるんだな。
これはspike準拠ですと。

じゃあそこは気にせず、0x1000にブートローダーを置けば良いわけだ。

@tomoyuki-nakabayashi
Copy link
Owner Author

https://github.com/fractalclone/zephyr-riscv

zephyrはriscv-qemu sifive modelで動作する。

@tomoyuki-nakabayashi
Copy link
Owner Author

/**
 *
 * @brief Copy the data section from ROM to RAM
 *
 * This routine copies the data section from ROM to RAM.
 *
 * @return N/A
 */
void _data_copy(void)
{
	memcpy(&__data_ram_start, &__data_rom_start,
		 ((u32_t) &__data_ram_end - (u32_t) &__data_ram_start));
#ifdef CONFIG_APPLICATION_MEMORY
	memcpy(&__app_data_ram_start, &__app_data_rom_start,
		 ((u32_t) &__app_data_ram_end - (u32_t) &__app_data_ram_start));
#endif
}

あー、なるほど、ROMからデータをRAMにコピーしてあげるのか。で、その後は?

@tomoyuki-nakabayashi
Copy link
Owner Author

tomoyuki-nakabayashi commented Nov 2, 2018

zephyrをビルドして、動かしてみよう。

https://github.com/fractalclone/zephyr-riscv

$ wget https://github.com/zephyrproject-rtos/meta-zephyr-sdk/releases/download/0.9.1/zephyr-sdk-0.9.1-setup.run
$ chmod +x zephyr-sdk-0.9.1-setup.run
$ sudo ./zephyr-sdk-0.9.1-setup.run
Verifying archive integrity... All good.
Uncompressing SDK for Zephyr  100%  
Enter target directory for SDK (default: /opt/zephyr-sdk/): 
Installing SDK to /opt/zephyr-sdk
Creating directory /opt/zephyr-sdk
Success
 [*] Installing x86 tools... 
 [*] Installing arm tools... 
 [*] Installing arc tools... 
 [*] Installing iamcu tools... 
 [*] Installing nios2 tools... 
 [*] Installing xtensa tools... 
 [*] Installing riscv32 tools... 
 [*] Installing additional host tools... 
Success installing SDK. SDK is ready to be used.
$ export ZEPHYR_SDK_INSTALL_DIR=/opt/zephyr-sdk
$ export ZEPHYR_GCC_VARIANT=zephyr
$ cd samples/hello_world
$ make BOARD=qemu_riscv32
$ ls -lha outdir/qemu_riscv32/
-rwxrwxr-x  1 tomoyuki tomoyuki 2.1G 11月  2 14:28 zephyr.bin
-rwxrwxr-x  1 tomoyuki tomoyuki 198K 11月  2 14:28 zephyr.elf
-rw-rw-r--  1 tomoyuki tomoyuki  491 11月  2 14:28 zephyr.lnk
-rw-rw-r--  1 tomoyuki tomoyuki 119K 11月  2 14:28 zephyr.lst
-rw-rw-r--  1 tomoyuki tomoyuki  60K 11月  2 14:28 zephyr.map
-rwxrwxr-x  1 tomoyuki tomoyuki 196K 11月  2 14:28 zephyr_prebuilt.elf
-rw-rw-r--  1 tomoyuki tomoyuki 3.5K 11月  2 14:28 zephyr.stat
-rwxrwxr-x  1 tomoyuki tomoyuki  11K 11月  2 14:28 zephyr.strip

自分でビルドしたriscv-toolchainも利用できるが、OSをnoneで作っておかないといけないみたい。linux用にツールチェーンを構築してしまったので、大人しくzephyrのSDKを使う。
zephyr-sdk、qemuも入っているのか。

@tomoyuki-nakabayashi
Copy link
Owner Author

tomoyuki-nakabayashi commented Nov 2, 2018

うーん、.binが最終成果物かなぁ?0x80000000にBSS領域とかあるから、生だと2.1GBになるのか…?

$ readelf -S outdir/qemu_riscv32/zephyr.elf
There are 23 section headers, starting at offset 0x31380:

Section Headers:
  [Nr] Name              Type            Addr     Off    Size   ES Flg Lk Inf Al
  [ 0]                   NULL            00000000 000000 000000 00      0   0  0
  [ 1] vector            PROGBITS        00001000 000074 00041c 00  AX  0   0  4
  [ 2] reset             PROGBITS        80000000 000490 000004 00  AX  0   0  4
  [ 3] exceptions        PROGBITS        80000004 000494 0002a0 00  AX  0   0  4
  [ 4] text              PROGBITS        800002a4 000734 001904 00 WAX  0   0  4
  [ 5] devconfig         PROGBITS        80001ba8 002038 000024 00  WA  0   0  4
  [ 6] rodata            PROGBITS        80001bcc 00205c 000540 00   A  0   0  4
  [ 7] datas             PROGBITS        8000210c 00259c 00001c 00  WA  0   0  4
  [ 8] initlevel         PROGBITS        80002128 0025b8 000024 00  WA  0   0  4
  [ 9] bss               NOBITS          80002150 0025dc 000218 00  WA  0   0  8
  [10] noinit            NOBITS          80002370 0025dc 000e00 00  WA  0   0 16
  [11] .debug_info       PROGBITS        00000000 0025dc 01147b 00      0   0  1
  [12] .debug_abbrev     PROGBITS        00000000 013a57 00497d 00      0   0  1
  [13] .debug_aranges    PROGBITS        00000000 0183e0 000980 00      0   0 16
  [14] .debug_line       PROGBITS        00000000 018d60 009fe4 00      0   0  1
  [15] .debug_str        PROGBITS        00000000 022d44 002936 01  MS  0   0  1
  [16] .comment          PROGBITS        00000000 02567a 000011 01  MS  0   0  1
  [17] .debug_frame      PROGBITS        00000000 02568c 001800 00      0   0  4
  [18] .debug_ranges     PROGBITS        00000000 026e90 0010d8 00      0   0 16
  [19] .debug_loc        PROGBITS        00000000 027f68 0061d6 00      0   0  1
  [20] .symtab           SYMTAB          00000000 02e140 0016b0 10     21 102  4
  [21] .strtab           STRTAB          00000000 02f7f0 001aba 00      0   0  1
  [22] .shstrtab         STRTAB          00000000 0312aa 0000d3 00      0   0  1
Key to Flags:
  W (write), A (alloc), X (execute), M (merge), S (strings)
  I (info), L (link order), G (group), T (TLS), E (exclude), x (unknown)
  O (extra OS processing required) o (OS specific), p (processor specific)

むむむ。

@tomoyuki-nakabayashi
Copy link
Owner Author

どうもqemu-riscv用のバイナリは手抜きしているっぽい。
pulpino向けだと、spiで送信するバイナリが出力されるみたいね。

@tomoyuki-nakabayashi
Copy link
Owner Author

とりあえず、elfとvirtの組み合わせでいいから、UARTでhello worldするか。

@tomoyuki-nakabayashi
Copy link
Owner Author

$ make
cargo xbuild --target riscv32imac-unknown-none-elf
   Compiling version_check v0.1.5
   Compiling spin v0.4.10
error[E0599]: no method named `compare_and_swap` found for type `core::sync::atomic::AtomicBool` in the current scope
   --> /home/tomoyuki/.cargo/registry/src/github.com-1ecc6299db9ec823/spin-0.4.10/src/mutex.rs:157:25
    |
157 |         while self.lock.compare_and_swap(false, true, Ordering::Acquire) != false
    |                         ^^^^^^^^^^^^^^^^

rust-lang/log#285

RISC-V、Atomicサポートされていない?

@tomoyuki-nakabayashi
Copy link
Owner Author

@tomoyuki-nakabayashi
Copy link
Owner Author

ぐげぇ、uart_16550、x86_64 crateに依存しとるやんけ。

@tomoyuki-nakabayashi
Copy link
Owner Author

$ /opt/riscv-qemu/bin/qemu-system-riscv32 -nographic -machine virt -kernel rvos-rs
a

OK!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant