Linux に平行してベアメタルプログラムを走らせる のバックアップ(No.4)

更新


公開メモ

CPU でリアルタイム処理をしたい

UI や通信を Linux で行いつつ、
リアルタイム性能の必要な処理を別途動かしたい。

Zynq には CPU が2つあるので、1つを Linux に、 1つをベアメタルに使えば完全な並列処理ができるはず?

http://www.wiki.xilinx.com/Real-Time+Linux によれば、
IRQ Affinity という機能で、Linux で受け取る IRQ を特定の CPU に回すという方法もあるらしい。そちらの CPU を常時 Idle にしておき、 割り込み処理中の追加割り込みを禁止できるなら、Linux だけでリアルタイム処理ができるのかもしれないけれど、 そういう話にはなっていないようで。

資料

シンプルな AMP : 2 つの Zynq SoC プロセッサ上で動作する Linux およびベアメタルシステム
https://japan.xilinx.com/support/documentation/application_notes/j_xapp1078-amp-linux-bare-metal.pdf

上記に対する最新の補遺
http://www.wiki.xilinx.com/XAPP1078+Latest+Information

むしろこっちが最新か?
http://www.wiki.xilinx.com/Multi-OS+Support+(AMP+%26+Hypervisor)

FPGAマガジンの16に FreeRTOS をメインに動かして、 後から Linux を起動する方法が書かれている。

OpenAmp
https://github.com/OpenAMP/open-amp
http://www.wiki.xilinx.com/OpenAMP

これを読めばいいのかも。

概要

OpenAMP

https://github.com/OpenAMP/open-amp

CPU0 で走る Linux アプリから FreeRTOS や ベアメタル アプリを CPU1 で立ち上げたり、 そのリモートアプリとの間で通信をしたり、リモートアプリに Linux の資源を使わせたり するためのライブラリとのことで、まさに欲しかった機能と言えます。

  • remoteproc ホストからリモートのプロセスを制御する
  • RPMsg ホストとリモートとの間での通信手段を提供する

OpenAMP は petalinux と、libmetal ライブラリの上に成り立っています。

libmetal

linux, freertos, baremetal で共通に使えるライブラリ API を提供し、 デバイスへのアクセスや割り込み制御、メモリ確保、その他ユーティリティ機能を提供するもの のようです。

Petalinux

概要

オープンソースの Xilinx Linux とは別物。Xilinx が開発している。

登録は必要だが無料で使える。

ダウンロード

https://www.xilinx.com/support/download/index.html/content/xilinx/en/downloadNav/embedded-design-tools.html

ここから petalinux-v2016.4-final-installer1.run のようなファイルを落とす。

https://japan.xilinx.com/support/answers/68370.html

によれば カーネルバージョンは 4.6 だそうだ。

インストール

ここでは VirtualBox 内の Debian9 stretch に入れてみる

インストール方法は、
https://japan.xilinx.com/support/documentation-navigation/development-tools/software-development/petalinux-tools.html から ug1144-petalinux-tools-reference-guide.pdf にあります。

先に petalinux が要求するパッケージを入れます。 diffstat xvfb chrpath xterm libtool は上記ガイドに書かれていませんが、 インストール時に入れろと言われます。

LANG:console
$ sudo apt-get install build-essential binutils ncurses-dev u-boot-tools file
$ sudo apt-get install tofrodos iproute2 gawk net-tools libncurses5-dev tftp tftpd-hpa zlib1g-dev libssl-dev flex bison libselinux1
$ sudo apt-get install diffstat xvfb chrpath xterm libtool

petalinux のインストーラは巨大なバイナリを含んだ bash スクリプトです。 bash スクリプトなので実行権限が無くても bash に読ませれば実行できます。

インストーラを root で走らせると途中で以下のエラーが出ます。

ERROR: The extensible sdk cannot be installed as root.
ERROR: Failed to install Yocto SDK for zynqMP.

一方、インストーラを 非root で走らせると、/opt などのルート権限の 必要なフォルダにはインストールできません。

基本的に、ユーザーのホーム以下へのインストールが期待されているようです?
ここでは ~/z-turn/petalinux へインストールすることにしました。

ところでこれ、元のインストールファイルが大きく、/tmp に展開される ファイルも大きくて、インストール後も大きいので、インストールファイル ダウンロード前の VirtualBox の空き容量が 30GB 弱だと途中でぎりぎり Disk Full になりました。35GB もあれば大丈夫そうですが、かなり 待たされた後でエラーが出るので、余裕を持った方が精神的に楽です。

インストール後は 15.9 GB でした。

LANG:console
$ bash petalinux-v2016.4-final-installer1.run -h
 PetaLinux installer.
 
 Usage:
   petalinux-v2016.4-final-installer1.run [--log <LOGFILE>] [INSTALL_DIR]
 
 Options:
   --log <LOGFILE>     specify where the logfile should be created.
                       it will be petalinux_installation_log
                       in your working directory by default.
   [INSTALL_DIR]       specify the directory where you want to
                       install the tool kit. If not specified,
                       it will install to your working directory.

$ mkdir ~/z-turn/petalinux
$ # インストールに sudo はしません
$ bash petalinux-v2016.4-final-installer1.run ~/z-turn/petalinux
 INFO: Checking installer checksum...
 INFO: Extracting PetaLinux installer...
 INFO: Installing PetaLinux...
 INFO: Checking PetaLinux installer integrity...
 INFO: Extracting Installation files...
                                       # ここまで、ものすごく時間がかかります
 LICENSE AGREEMENTS
 
 PetaLinux SDK contains software from a number of sources.  Please review
 the following licenses and indicate your acceptance of each to continue.
 
 You do not have to accept the licenses, however if you do not then you may
 not use PetaLinux SDK.
 
 Use PgUp/PgDn to navigate the license viewer, and press 'q' to close
 
 Press Enter to display the license agreements
 Do you accept Xilinx End User License Agreement? [y/N] > y
 Do you accept Webtalk Terms and Conditions? [y/N] > y
 Do you accept Third Party End User License Agreement? [y/N] > y
 INFO: Checking installation environment requirements...
 INFO: Checking free disk space
 INFO: Checking installed tools
 ERROR: You are missing the following system tools required by PetaLinux:
 
  - libtool
 Please check PetaLinux installation guide - required tools and libraries 
 pacakge section for detailed information

 INFO: Checking installed development libraries
 Please install them with your operating system package manager, and try again
 INFO: Checking network and other services
 INFO: Installing PetaLinux SDK to "/home/osamu/z-turn/petalinux/."
 INFO: Installing PetaLinux zynqMP Yocto SDK to "/home/osamu/z-turn/petalinux/./components/yocto/source/aarch64"...
 PetaLinux Extensible SDK installer version 2016.4
 =================================================
 You are about to install the SDK to "/home/osamu/z-turn/petalinux/components/yocto/source/aarch64".
 Proceed[Y/n]? Y
 Extracting SDK...............................done
 Setting it up...
 Extracting buildtools...
 done
 SDK has been successfully set up and is ready to be used.
 Each time you wish to use the SDK in a new shell session, you need to source the environment setup script e.g.
  $ . /home/osamu/z-turn/petalinux/components/yocto/source/microblaze_lite/environment-setup-microblazeel-v10.0-bs-cmp-ml-xilinx-linux
 INFO: PetaLinux Yocto SDK for microblaze has been successfully installed.
 INFO: PetaLinux SDK has been installed to /home/osamu/z-turn/petalinux/.

libtool はすでに入っているのだけれど・・・とりあえず無視して続行します。

動作環境の構築

libtoolize を libtool として起動できるようにする
LANG:console
$ source z-turn/petalinux/settings.sh
 PetaLinux environment set to '/home/osamu/z-turn/petalinux'
 WARNING: /bin/sh is not bash!
 bash is PetaLinux recommended shell. Please set your default shell to bash.
 INFO: Checking free disk space
 INFO: Checking installed tools
 ERROR: You are missing the following system tools required by PetaLinux:
 
  - libtool
 Please check PetaLinux installation guide - required tools and libraries 
 pacakge section for detailed information
 
 INFO: Checking installed development libraries
 Please install them with your operating system package manager, and try again
 INFO: Checking network and other services
 WARNING: No tftp server found - please refer to "PetaLinux SDK Installation Guide" 
 for its impact and solution
$ sudo apt-get install libtool
 libtool はすでに最新バージョン (2.4.6-2) です。
 アップグレード: 0 個、新規インストール: 0 個、削除: 0 個、保留: 336 個。
$ libtool
 -bash: libtool: コマンドが見つかりません
$ libtoolize --version
 libtoolize (GNU libtool) 2.4.6
 Written by Gary V. Vaughan <gary@gnu.org>, 2003
 
 Copyright (C) 2015 Free Software Foundation, Inc.
 This is free software; see the source for copying conditions.  There is NO
 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ apt-get install libtool-bin
$ libtool
 -bash: libtool: コマンドが見つかりません
$ # 別名を付けるだけでいいという噂も?
$ sudo ln -s /usr/bin/libtoolize /usr/local/bin/libtool
$ source z-turn/petalinux/settings.sh
 PetaLinux environment set to '/home/osamu/z-turn/petalinux'
 WARNING: /bin/sh is not bash!
 bash is PetaLinux recommended shell. Please set your default shell to bash.
 INFO: Checking free disk space
 INFO: Checking installed tools
 INFO: Checking installed development libraries
 INFO: Checking network and other services
 WARNING: No tftp server found - please refer to "PetaLinux SDK Installation Guide" 
 for its impact and solution
$ echo $PETALINUX
 /home/osamu/z-turn/petalinux

パッケージ使用状況のサーベイを off にする

LANG:console
$ z-turn/petalinux/tools/common/petalinux/bin/petalinux-util --webtalk off 
 INFO: Turn off webtalk feature!

BSP (board support package) の準備

z-turn ボード用にビルドしたいのだけれど、公式にはサポートされていないので、 試しに zedboard 用を落としてみた。

LANG:console
$ file avnet-digilent-zedboard-v2016.4-final1.bsp
 avnet-digilent-zedboard-v2016.4-final1.bsp: gzip compressed data, last modified: Fri Dec 16 03:02:22 2016, from Unix
$ tar fxz avnet-digilent-zedboard-v2016.4-final1.bsp
$ cd avnet-digilent-zedboard-2016.4/
$ ls
 components  config.project  hardware  pre-built  project-spec

かなりいろいろ入ってる。。。

これを元にプロジェクトを作るのというのがチュートリアルだけれど、 ここではそんな便利なものはないので、zynq 用の汎用テンプレートを使って作る。

LANG:console
$ cd ~/z-turn
$ petalinux-create -t project --template zynq -n petalinux-zturn
 INFO: Create project: petalinux-zturn
 INFO: New project successfully created in /home/osamu/z-turn/petalinux-zturn
$ ls petalinux-zturn/project-spec/
 attributes  configs  hw-description  meta-user

どうやら project-spec/ というのが petalinux のプロジェクト本体のようだ。

動作環境を整える

ここでは VirtualBox 内の Debian9 stretch に arm のエミュレータを入れて実行してみる。

まず 電気回路/z-turn/Linuxの設定 の流れで Linux ルートファイルシステムを作る。 カーネルが 4.6 なので jessie ではなく stretch を入れた。 (stretch では less や、ifconfig のための net-tools を別途インストールする必要がありました。)

LANG:console
$ sudo chroot ~/z-turn/rootfs

これで、qemu 上での root 権限での実行に切り替わります。


Counter: 20462 (from 2010/06/03), today: 1, yesterday: 0