måndag 24 januari 2011

Building WiiMC on a Mac OS X

WiiMC is a homebrew program for Wii.
These are my bash notes on how to downloads a devkitPPC environment, install some libraries needed, and to show where to patch a file to make it compile.

Here are the bash commands...   Use them with care...  :-)



 
export WIIROOT=$(pwd)
 
svn checkout http://wiimc.googlecode.com/svn/trunk/ wiimc-read-only
svn checkout http://libext2fs-wii.googlecode.com/svn/trunk/ libext2fs-wii-read-only
svn checkout http://grrlib.googlecode.com/svn/trunk/GRRLIB/lib/zlib
 
export WIIMCSRC=$WIIROOT/wiimc-read-only
 
 
mkdir -p $WIIROOT/devkitpro
export DEVKITPRO=$WIIROOT/devkitpro
 
echo THIS IS IMPORTANT
echo export DEVKITPPC=$DEVKITPRO/devkitPPC
echo export PATH=$DEVKITPPC/bin:$PATH
 
export DEVKITPPC=$DEVKITPRO/devkitPPC
export PATH=$DEVKITPPC/bin:$PATH
 
 
mkdir -p $WIIROOT/download
cd $WIIROOT/download
 
DEVKITURL=http://sourceforge.net/projects/devkitpro/files
 
(
wget -c ${DEVKITURL}/devkitPPC/devkitPPC_r22-osx.tar.bz2 &
wget -c ${DEVKITURL}/libogc/libogc-1.8.6.tar.bz2 &
wget -c ${DEVKITURL}/examples/wii/wii-examples-20100930.tar.bz2 &
wget -c ${DEVKITURL}/examples/gamecube/gamecube-examples-20100930.tar.bz2 &
wget -c ${DEVKITURL}/libfat/libfat-ogc-1.0.8.tar.bz2 &
 
# wget -c ${DEVKITURL}/portlibs/zlib-1.2.4-ppc.tar.bz2 &
wget -c ${DEVKITURL}/portlibs/mxml-2.6-ppc.tar.bz2 &
wget -c ${DEVKITURL}/portlibs/libpng-1.4.1-ppc.tar.bz2 &
wget -c ${DEVKITURL}/portlibs/jpeg8a-ppc.tar.bz2 &
wget -c ${DEVKITURL}/portlibs/freetype-2.4.2-ppc.tar.bz2 &
wait
)
 
diff <(md5 *bz2) <(cat << EOF
MD5 (devkitPPC_r22-osx.tar.bz2) = 385300a31c4c2cc6890adfb8f220674c
MD5 (freetype-2.4.2-ppc.tar.bz2) = 0e91ad37ead10137cc9f8d2f3454a245
MD5 (gamecube-examples-20100930.tar.bz2) = e9ef264a5c7acd6ef6144b676527bacd
MD5 (jpeg8a-ppc.tar.bz2) = ef4c03019ade885dbba05982e777ea2b
MD5 (libfat-ogc-1.0.8.tar.bz2) = 495cb164afa9ca420fd21d4c78c0723f
MD5 (libogc-1.8.6.tar.bz2) = 5be16c7c972e8c92e637220adb123be1
MD5 (libpng-1.4.1-ppc.tar.bz2) = 29a9a6c754d32990f542990d3d38e715
MD5 (mxml-2.6-ppc.tar.bz2) = b196036aa9f0b310efb6bdc4b02614dc
MD5 (wii-examples-20100930.tar.bz2) = 5a37fb6cc1704f43cb3813ddbb964a24
EOF) 
 
#MD5 (zlib-1.2.4-ppc.tar.bz2) = c952918d7fb3e52e8fa66fa09f2edf87
 
 
if [ ! $? ] ; then
    echo The checksums on the downloaded files were wrong.
    exit 1
fi
 
cd $WIIROOT/download
 
tar -C $DEVKITPRO -xjf devkitPPC_r22-osx.tar.bz2
 
mkdir $DEVKITPRO/libogc
tar -C $DEVKITPRO/libogc -xjf libogc-1.8.6.tar.bz2
tar -C $DEVKITPRO/libogc -xjf libfat-ogc-1.0.8.tar.bz2
 
mkdir -p $DEVKITPRO/examples/{wii,gamecube}
tar -C $DEVKITPRO/examples/wii  -jxf wii-examples-20100930.tar.bz2
tar -C $DEVKITPRO/examples/gamecube  -jxf gamecube-examples-20100930.tar.bz2
 
mkdir -p $DEVKITPRO/portlibs/ppc
#tar -C $DEVKITPRO/portlibs/ppc -xjf zlib-1.2.4-ppc.tar.bz2
tar -C $DEVKITPRO/portlibs/ppc -xjf mxml-2.6-ppc.tar.bz2
tar -C $DEVKITPRO/portlibs/ppc -xjf libpng-1.4.1-ppc.tar.bz2
tar -C $DEVKITPRO/portlibs/ppc -xjf jpeg8a-ppc.tar.bz2
tar -C $DEVKITPRO/portlibs/ppc -xjf freetype-2.4.2-ppc.tar.bz2
 
 
############################################
 
 
cd $WIIMCSRC/libs/libntfs
make
make install
 
# remember: the powerpc-eabi commands must be in the path 
# so that 'configure' can find them)
 
for lib in fribidi libexif pcre libiconv ; do
  echo Building $lib.  Press RETURN to start
  read
  cd $WIIMCSRC/libs/$lib
  ./configure --host=powerpc-eabi --prefix="${DEVKITPRO}/portlibs/ppc" --libdir="${DEVKITPRO}/portlibs/ppc/lib" --disable-shared
  make
  make install
done
 
cd $WIIROOT/libext2fs-wii-read-only
make
make install
 
 
#install zlib 1.2.5 instead of 1.2.4 which is what we got from the devkitpro site
cd $WIIROOT/zlib
make
make install
cp $DEVKITPRO/libogc/include/{zconf,zlib}.h $DEVKITPRO/portlibs/ppc/include
cp $DEVKITPRO/libogc/lib/wii/libz.a $DEVKITPRO/portlibs/ppc/lib/libz.a 
#sed -i -e 's/1\.2\.4/1\.2\.5/' $DEVKITPRO/portlibs/ppc/lib/pkgconfig/zlib.pc
 
 
# fix missing include file in wiimc.cpp
 
===================================================================
--- source/wiimc.cpp    (revision 802)
+++ source/wiimc.cpp    (working copy)
@@ -14,6 +14,7 @@
 #include <dirent.h>
 #include <wiiuse/wpad.h>
 #include <di/di.h>
+#include <sys/iosupport.h>
 
 #include "utils/FreeTypeGX.h"
 #include "utils/gettext.h"
 
 
 
cd $WIIMCSRC
make

fredag 10 december 2010

GDB reverse debugging

GDB reverse debugging


Let's start with a simple C++ program in the file gdb-test.cpp:


#include <iostream>
 
const unsigned int SIZE = 10;
 
int main() {
 
  int b[SIZE];
 
  // intitialize
  for(int i=0; i <= SIZE; i++) {
    b[i]=0;
  }
 
  std::cout << "done!" << std::endl;
  return 0;
}

Let's comple and run it.


$ g++ -g gdb-test.cpp -o gdb-test
$ ./gdb-test

??? It doesn't stop!


Let's load it into gdb.


$ gdb ./gdb-test
GNU gdb (GDB) 7.1-ubuntu
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
[...]
Reading symbols from /home/larsr/gdb-test...done.
(gdb)

Now run it:


(gdb) r
Starting program: /home/larsr/gdb-test

Still dosn't stop! Stop it with ctrl-c:


^C
Program received signal SIGINT, Interrupt.
0x08048704 in main () at gdb-test.cpp:11
11    for(int i=0; i <= SIZE; i++) {
(gdb)

Let's look at the variable i.


(gdb) print i
$1 = 4
(gdb)

Looks normal enough! But strange that it didn't get further than 4... Well, let's run some more, break, and look at i.


(gdb) continue
Continuing.
^C
Program received signal SIGINT, Interrupt.
0x080486fc in main () at gdb-test.cpp:11
11    for(int i=0; i <= SIZE; i++) {
(gdb) print i
$2 = 7
(gdb) c
Continuing.
^C
Program received signal SIGINT, Interrupt.
0x080486f8 in main () at gdb-test.cpp:11
11    for(int i=0; i <= SIZE; i++) {
(gdb) print i
$4 = 6
(gdb)

What? First i was 7 and then it was 6! How did that happen? Lets start to record, and run some more!


(gdb) record
(gdb) c
Continuing.
^C
Program received signal SIGINT, Interrupt.
main () at gdb-test.cpp:11
11    for(int i=0; i <= SIZE; i++) {
(gdb) print i
$6 = 2
(gdb)

Something has modified i to become smaller! When did this happen? Let's watch i and go backwards.


(gdb) reverse-continue 
Continuing.
Hardware watchpoint 1: i
 
Old value = 2
New value = 1
main () at gdb-test.cpp:11
11    for(int i=0; i <= SIZE; i++) {

This makes sense. We are walking backswards, and in that direction we had a value of 2 which turned into 1. Let's keep going backwards.


(gdb) reverse-continue 
Continuing.
Hardware watchpoint 1: i
 
Old value = 1
New value = 0
main () at gdb-test.cpp:11
11    for(int i=0; i <= SIZE; i++) {
(gdb) reverse-continue 
Continuing.
Hardware watchpoint 1: i
 
Old value = 0
New value = 10
0x080486eb in main () at gdb-test.cpp:12
12      b[i]=0;
(gdb)

Here we see that as we went backwards, we went from 0 to 10. That means that when we ran forward, i went from 10 to 0, and it happened when we executed


b[i]=0;

Could it be that writing to b[10] overwrites the memory of i? Where are they located in memory?


(gdb) print &i
$7 = (int *) 0xbffff7bc
(gdb) print &b[i]
$8 = (int *) 0xbffff7bc
(gdb)

Yup, they are at the same address. Why? Doh! An array indexing error! b[10] is not within the array bounds! The last element is b[9].


Line 11 should use < instead of <= and read


for(int i=0; i <  SIZE; i++) {

Case closed.


An ugly observation is that programs like this bug out silently and "work" if you change the program to be initialized to 100 (or something bigger than SIZE).

Tic-tac-toe in python

Represent a board with a nine character string with spaces, "X", and "O". The rows of the board are concatenated to make the string, so the board


 X |   |   
---+---+---
   | O |   
---+---+---
   | X | O 


is the string "X   O  XO".

Given a board string, the function tic() returns the next board.  However, if the opponent can win (provided he makes the right moves), then tic() doesn't try to fight back, and may return bad moves.  A smarter program might continue to fight back as long as the game is not actually over.

fredag 26 november 2010

kontrollsiffra personnummer oneliner in python

def pnr(nr,d=2,c=0,i=0): return (i==9) and 10-(c%10) or pnr(str(nr),3-d,c+(d*int(nr[i]))%10+(d*int(nr[i]))/10,i+1)



def pnr(p):
    a,p=[0,2,4,6,8,1,3,5,7,9],[int(i) for i in p]
    return 10-(a[p[0]]+p[1]+a[p[2]]+p[3]+a[p[4]]+p[5]+a[p[6]]+p[7]+a[p[8]])%10



print "sista siffran = %d" % pnr("6408233234"[:-1])

torsdag 25 november 2010

Setup IPSec between two hosts

# based on http://www.ipsec-howto.org/x304.html


key() ( bits=$1; dd if=/dev/random count=$((bits/8)) bs=1| xxd -ps )

host1=2001:16e8:ff4e::1
host2=2001:16e7:cd3c::223:77ff:fe18:1421

cat > setkey.conf <
#!/usr/sbin/setkey -f

# Configuration for $host1

# Flush the SAD and SPD
flush;
spdflush;

# set the authentication header
# AH SAs using 128 bit long keys
add $host2 $host1 ah 0x200 -A hmac-md5  0x$(key 128);
add $host1 $host2 ah 0x300 -A hmac-md5  0x$(key 128);

# ESP SAs using 192 bit long keys (168 + 24 parity)
add $host2 $host1 esp 0x201 -E 3des-cbc  0x$(key 192);
add $host1 $host2 esp 0x301 -E 3des-cbc  0x$(key 192);

# Security policies
spdadd $host2 $host1 any -P out ipsec
           esp/transport//require
           ah/transport//require;

spdadd $host1 $host2 any -P in ipsec
           esp/transport//require
           ah/transport//require;

EOF

sudo setkey -f setkey.conf

# copy setkey.conf to the other computer (host2),
# and on the other computer swap the '-P in' and '-P out'
# in the spdadd and run the sudo setkey -f setkey.conf
# WARNING! You will not be able to contact the other
# computer if only one of them has enabled the IPSec rules
# with setkey.

måndag 1 november 2010

multiple users on one screen session

I got this info from here.

Suppose larsr and bill have accounts on a machine stella.
Thanks to 'screen' they can easily share a terminal window.

both log in to stella (for instace with ssh).

larsr starts screen

  • screen

and enables multiuser mode

  • CTRL-a :multiuser on

and permit bill to connect

  • CTRL-a :acladd bill


Now bill can connect to the screen by typing

  • screen -x larsr/

Note the '/'. If larsr has many screens, the screen id is entered after the '/'.

torsdag 21 oktober 2010


Building the LLVM based binary translator.


Lars.Rasmusson@sics.se 2010-10-21


1. Create clean build root file system


To make sure that we know what software we are using to build, we first create an entire root file system in a catalog. It containts its own build tools, independent of what the host OS itself uses.
Use debootstrap to install a bare bones root file system from a local repository:


sudo -s
BUILDROOT=$(pwd)/xenbuild
mkdir $BUILDROOT
debootstrap --variant=buildd --arch i386 lucid $BUILDROOT http://se.archive.ubuntu.com/ubuntu

2. chroot into it, configure and install build tools and git


Set time zone, and use apt to install the build tools


chroot $BUILDROOT
export LC_ALL=C
hostname builder
dpkg-reconfigure tzdata
adduser --disabled-password larsr

Use a local repository and install a lot of packets


echo > /etc/apt/sources.list deb http://se.archive.ubuntu.com/ubuntu lucid main restricted universe
apt-get update
apt-get install -y wget nano mercurial gawk openssh-client libz-dev \
  gettext libssl-dev libx11-dev bin86 bcc libncurses5-dev python-dev \
  texinfo bridge-utils uuid-dev git-core man iasl m4 flex bison cmake

3. check out xen from git on mac (edit on mac?)


Initialize my git user name


su larsr
cd $HOME
cat >> .gitconfig <<EOF
[user]
        name = Lars Rasmusson
        email = Lars.Rasmusson@sics.se
EOF
 
git clone ssh://lra@abakus.sics6.se/home/lra/src/xen-4.0-testing.git

4. Building and installing the Xen kernel


NOTE! These steps are only necessary if you are not already running xen on your testing machine.


4.1 Build


Prepare a new branch llvm-domain and check out a specific version of Xen. In the buildroot tree, do


cd ~/xen-4.0-testing
git checkout -b llvm-domain RELEASE-4.0.1
git clean -fd
make -j8 xen
make -j8 tools

4.2 Install


In the real root tree, copy the new xen-image to the /boot directory, and configure the bootloader (grub/grub2/whatever) to load the newly compiled xen.


First pack the xen files into a tar file and then into a deb. It makes it easier to remove or update later.


tar -czf xen.tgz -C dist/install/ .
fakeroot alien --to-deb xen.tgz

Then install the files in the real root file system. The update-rc.d commands makes xend and xendomains start when the system boots.


dpkg -i /home/larsr/xenbuild/home/larsr/xen-4.0-testing/xen_1-2_all.deb 
update-rc.d xend defaults
update-rc.d xendomains defaults

I'm running Ubuntu 10.04 Lucid Lynx which uses the grub2 boot loader. I installed Xen in grub by putting the following into /etc/grub.d/35_xen


#!/bin/sh
exec tail -n +3 $0
# This file is an example on how to add custom entries
 
menuentry "Xen 4.0 / Ubuntu 10.04 kernel 2.6.31.13 pvops" {
    insmod ext2
    set root=(hd0,1)
    multiboot (hd0,1)/xen-4.0.gz com1=115200,8n1 console=com1,vga
    module (hd0,1)/vmlinuz-2.6.31.13 console=tty0 console=hvc0 root=/dev/mapper/cluster5-root ro
    module (hd0,1)/initrd.img-2.6.31.13
    savedefault
}

and then run


update-grub

A linux running in Xen gets its console on hvc0, and for Ubuntu to start a getty on hvc0, put hvc0.conf in /etc/init/hvc0.conf


# hvc0 - getty
#
# This service maintains a getty on Xen's ttyhvc0 from the point the system is
# started until it is shut down again.
 
start on stopped rc RUNLEVEL=[2345]
stop on runlevel [!2345]
 
respawn
exec /sbin/getty -8 115200 hvc0

Now you should be able to reboot and select the xen kernel from grub's boot menu. When you have booted, verify with


xm info

that Xen and xend are running.


6. build stub domain


This is done in the chrooted xenbuild directory.
First we build a stub domain, which makes the Xen Makefiles pull in several packages such as lwip, newlib, etc. and then compile mini-os which uses the packages.


D=~/xen-4.0-testing/stubdom
 
cd $D
make c-stubdom

Now the mini-os object files are compiled. We put them for convenience into a library


MO=$(pwd)/mini-os-x86_32-c
ar cr $D/../extras/minios.a  $MO/{blkfront,events,fbfront,fs-front,gntmap,gnttab,hypervisor,kernel,lock,main,mm,netfront,pcifront,sched}.o \
$MO/lib/{ctype,math,printf,stack_chk_fail,string,sys,xmalloc,xs}.o $MO/xenbus/xenbus.o $MO/console/{console,xencons_ring}.o

Now we are ready to build the cross g++ for minios


9. Building a cross compiler


You can compile some c++ programs for minios using the system's normal gcc, but it can't use iostreams etc, because the header files for g++ depend on the linux header files, and will break when compiling for minios.


We need a special c++ compiler that uses the right system include files, and newlib instead of glibc, or else
include iostream will not work. A g++ compiled for linux relies on some definitions
in the ctype.h file. Those definitions do not exist in minios, and therefore the iostream header files
generated for a linux g++ will not work on a minios system.


To build a g++ for minios, the sysroot of mini os must exist.

It got created (above) when we did make c-stubdom in the stubdom directory in the xen repository.
The minios sysroot is placed in $HOME/xen-4.0-testing/stubdom/cross-root-i686


# build tool chain
TARGET=i686-xen-elf
INSTALL=$HOME/sw
GCC_VER=4.5.0
###
 
export PATH=$INSTALL/bin:$PATH
 
mkdir -p $INSTALL/build
cd $INSTALL/build
 
(
wget http://ftp.sunet.se/pub/gnu/gmp/gmp-5.0.1.tar.bz2 &
wget http://ftp.sunet.se/pub/gnu/mpfr/mpfr-2.4.2.tar.bz2 &
wget http://ftp.sunet.se/pub/gnu/gcc/infrastructure/mpc-0.8.1.tar.gz &
wait
)
 
tar -xjf gmp-5.0.1.tar.bz2
tar -xjf mpfr-2.4.2.tar.bz2
tar -xzf mpc-0.8.1.tar.gz
 
(
wget http://ftp.sunet.se/pub/gnu/binutils/binutils-2.20.tar.bz2 & 
wget http://ftp.sunet.se/pub/gnu/gcc/releases/gcc-$GCC_VER/gcc-core-$GCC_VER.tar.bz2 &
wget http://ftp.sunet.se/pub/gnu/gcc/releases/gcc-$GCC_VER/gcc-g++-$GCC_VER.tar.bz2 &
wget http://ftp.sunet.se/pub/gnu/gdb/gdb-7.1.tar.bz2 &
wait
)
tar -xjf binutils-2.20.tar.bz2
tar -xjf gcc-core-$GCC_VER.tar.bz2
tar -xjf gcc-g++-$GCC_VER.tar.bz2
tar -xjf gdb-7.1.tar.bz2
 
wget ftp://sources.redhat.com/pub/newlib/newlib-1.18.0.tar.gz
tar -xzf newlib-1.18.0.tar.gz
 
mkdir gmp-5.0.1/build; cd gmp-5.0.1/build
../configure --prefix=$INSTALL/build/gmp --disable-shared --enable-static
make -j8 && make install
cd ../../
mkdir mpfr-2.4.2/build; cd mpfr-2.4.2/build
../configure --prefix=$INSTALL/build/mpfr --with-gmp=$INSTALL/build/gmp \
   --disable-shared --enable-static
make -j8 && make install
cd ../../
mkdir mpc-0.8.1/build; cd mpc-0.8.1/build
../configure --prefix=$INSTALL/build/mpc --with-gmp=$INSTALL/build/gmp \
   --with-mpfr=$INSTALL/build/mpfr --disable-shared --enable-static
make -j8 && make install
cd ../../
 
patch -l -p0 <<EOF
--- binutils-2.20/gas/as.h      2010-06-15 18:49:19.771279712 +0000
+++ binutils-2.20/gas/as.h      2010-06-15 18:50:10.400278790 +0000
@@ -238,7 +238,7 @@
 #define know(p) gas_assert(p)  /* Verify our assumptions!  */
 #endif /* not yet defined */
 #else
-#define know(p)                        /* know() checks are no-op.ed  */
+#define know(p)        do {} while(0)  /* know() checks are no-op.ed  */
 #endif
 
 /* input_scrub.c */
EOF
 
cd $INSTALL/build
mkdir binutils-2.20/build; cd binutils-2.20/build
../configure \
   --prefix=$INSTALL --target=$TARGET --enable-interwork \
   --enable-multilib --disable-nls --disable-shared --disable-threads \
   --with-sysroot=$HOME/xen-4.0-testing/stubdom/cross-root-i686/i686-xen-elf \
   --with-gcc --with-gnu-as --with-gnu-ld
make -j8 && make install
cd ../../
 
mkdir gcc-$GCC_VER/build; cd gcc-$GCC_VER/build
../configure --prefix=$INSTALL --target=$TARGET --enable-interwork \
   --disable-multilib --enable-languages=c --with-newlib --disable-nls \
   --disable-shared --disable-threads --with-gnu-as --with-gnu-ld \
   --with-gmp=$INSTALL/build/gmp --with-mpfr=$INSTALL/build/mpfr \
   --with-mpc=$INSTALL/build/mpc \
   --without-headers
make -j8 all-gcc && make install-gcc
cd ../../
 
mkdir newlib-1.18.0/build; cd newlib-1.18.0/build
../configure --prefix=$INSTALL --target=$TARGET --enable-interwork \
   --enable-multilib --with-gnu-as --with-gnu-ld --disable-nls 
make -j8 && make install
cd ../../
 
cd gcc-$GCC_VER/build
rm -rf *
../configure \
   --prefix=$INSTALL --target=$TARGET --enable-interwork \
   --disable-multilib --enable-languages=c,c++ --with-newlib --disable-nls \
   --disable-shared --disable-threads --with-gnu-as --with-gnu-ld \
   --with-gmp=$INSTALL/build/gmp --with-mpfr=$INSTALL/build/mpfr \
   --with-mpc=$INSTALL/build/mpc \
   --with-sysroot=$HOME/xen-4.0-testing/stubdom/cross-root-i686/i686-xen-elf 
make -j8 && make install
cd ../../

Now the cross compiler and build tools for mini-os are installed in ~/sw.


9.1 Building a stub domain with the new g++ compiler


Here we assume that there are two c++ files in stubdom/c called main.cpp and mi.cpp.


We use the g++ and ld compiled for mini os.


GPP=i686-xen-elf-g++
LD=i686-xen-elf-ld

Compile the source files


cd $D
$GPP c/main.cpp -c -o c/main.o
$GPP c/mi.cpp -c -o c/mi.o

Link with the mini os object files and our own libstdc++.


$LD -r -d \
-m elf_i386 \
$D/mini-os-x86_32-c/arch/x86/x86_32.o \
-\( \
$D/c/main.o \
$D/c/mi.o \
$D/../extras/mini-os/app.lds \
--undefined main \
-\) \
-whole-archive \
$D/../extras/minios.a \
-no-whole-archive \
$D/mini-os-x86_32-c/lwip.a \
$D/libxc-x86_32/libxenguest.a \
$D/libxc-x86_32/libxenctrl.a \
$D/mini-os-x86_32-c/arch/x86/libx86_32.a \
-L$D/cross-root-i686/i686-xen-elf/lib \
-lstdc++ \
-lc -lm -lz -lpci \
-L$D/cross-root-i686/lib/gcc/i686-xen-elf/4.5.0 \
-lgcc \
-o $D/mini-os-x86_32-c/mini-os.o

Create the final executable by placing the code segments where they should be and pack the file


$LD \
-m elf_i386 \
$D/mini-os-x86_32-c/mini-os.o  \
-T $D/../extras/mini-os/arch/x86/minios-x86_32.lds \
-o $D/mini-os-x86_32-c/mini-os
 
gzip -f -5 -c $D/mini-os-x86_32-c/mini-os >$D/mini-os-x86_32-c/mini-os.gz

Voila! It should run on xen (start it from dom0, not from inside the buildroot).


xm create /dev/null  name=minios disk=file:/home/larsr/disk2.img,/dev/sda,r vif="" kernel=/home/larsr/xenbuild/home/larsr/xen-4.0-testing/stubdom/mini-os-x86_32-c/mini-os.gz -c

10. Build LLVM for minios.


You have to use a g++ and gcc that is compiled for minios. To use LLVM inside the minios, we must have the c++ header files in the compilation paths, and the correct libraries included.


cd ~
git clone ssh://lra@abakus.sics6.se/home/lra/src/llvm.git llvm.git
wget http://www.sics.se/~lra/lars-llvm.patch
wget http://www.sics.se/~lra/include-llvm-Config-config.h.patch
cd llvm.git/
git checkout 27ff7ebce9a8d5dd2631156d910fd4898a028dbb
patch -p1 <../lars-llvm.patch 
 
mkdir build
cd build
../configure --prefix=$INSTALL --enable-targets=x86,x86_64 --disable-threads --target=i686-xen-elf
 
make BUILD_DIRS_ONLY=1 -j8
 
sed -i -e"s@int\t_EXFUN(getpagesize@size_t\t_EXFUN(getpagesize@" \
  $INSTALL/i686-xen-elf/include/sys/unistd.h
 
patch include/llvm/Config/config.h ../../include-llvm-Config-config.h.patch
# minios has MAP_ANON but not MAP_ANONYMOUS and no TEMP file support
sed -i -e"s@#define HAVE_MMAP_ANONYMOUS 1@//#define HAVE_MMAP_ANONYMOUS 1@" include/llvm/Config/config.h
sed -i -e"s@#define HAVE_MKDTEMP 1@//#define HAVE_MKDTEMP 1@" include/llvm/Config/config.h
sed -i -e"s@#define HAVE_MKSTEMP 1@//#define HAVE_MKSTEMP 1@" include/llvm/Config/config.h
sed -i -e"s@#define HAVE_MKTEMP 1@//#define HAVE_MKTEMP 1@" include/llvm/Config/config.h
 
make VERBOSE=1 CXX="i686-xen-elf-g++" CC="i686-xen-elf-gcc"  -j8 -C lib -k
# use `-k` because the dynamic linking example 'lib/Transforms/Hello' will fail because -ldl is not supported
 
rm Debug/lib/libLLVM{Support,System}.a
cd lib/Support
rm -rf Debug
make VERBOSE=1 CXX="i686-xen-elf-g++" CC="i686-xen-elf-gcc" -j8
cd ../..
 
cd lib/System
rm -rf Debug
make VERBOSE=1 CXX="i686-xen-elf-g++ -isystem ${D:?}/../extras/mini-os/include/posix" CC="i686-xen-elf-gcc"  -j8
cd ../..

We should also build the llvm tools to get llvm-config


make -C tools -j8

This breaks when it compiles llvm-mc, but llvm-config is compiled ok.


Now llvm-config is in build/Debug/bin/llvm-config


11. Building an LLVM program for minios


The following has to be defined in some c source file, because newlib doesn't provide
lseek and fstat correctly after the xen patches (which renames them).
The domain crashes if linked with crt{begin,end}.o so instead
declare the missing variable __dso_handle.
The code below could be placed in main.cpp, for instance.


extern off_t lseek64(int a, off_t b, int c);
off_t lseek(int a, off_t b, int c) {
    return lseek64 (a, b, c);
}
 
int fstat64(int a, struct stat* b);
int fstat(int a, struct stat* b) {
    return fstat64(a,b);
}
 
void *__dso_handle = 0;

12. To compile it


Suppose the program is in $D/c/jit-test.o and that llvm-config is in the path.


cd $D
 
i686-xen-elf-g++ $(~/llvm.git/build/Debug/bin/llvm-config --cflags) -c c/jit-test.cpp -o c/jit-test.o
 
i686-xen-elf-ld -r -d -m elf_i386 $D/mini-os-x86_32-c/arch/x86/x86_32.o -\( \
 $D/c/jit-test.o \
 $D/../extras/mini-os/app.lds --undefined main -\) \
 -whole-archive $D/../extras/minios.a -no-whole-archive \
 $D/mini-os-x86_32-c/lwip.a $D/libxc-x86_32/libxenguest.a $D/libxc-x86_32/libxenctrl.a \
 $D/mini-os-x86_32-c/arch/x86/libx86_32.a \
 -L/home/larsr/llvm.git/build/Debug/lib \
 -lLLVMX86Disassembler -lLLVMX86AsmParser -lLLVMX86AsmPrinter -lLLVMX86CodeGen -lLLVMSelectionDAG \
 -lLLVMAsmPrinter -lLLVMMCParser -lLLVMX86Info -lLLVMJIT -lLLVMExecutionEngine -lLLVMCodeGen -lLLVMScalarOpts \
 -lLLVMInstCombine -lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis -lLLVMTarget -lLLVMMC -lLLVMCore \
 -lLLVMSupport -lLLVMSystem \
 -L$INSTALL/i686-xen-elf/lib  -lstdc++ \
 -L$D/cross-root-i686/i686-xen-elf/lib -lc -lm -lz -lpci  \
 -L$INSTALL/lib/gcc/i686-xen-elf/4.5.0  -lgcc \
 -o $D/mini-os-x86_32-c/mini-os.o
 
 
i686-xen-elf-ld \
-m elf_i386 \
$D/mini-os-x86_32-c/mini-os.o  \
-T /home/larsr/xen-4.0-testing/extras/mini-os/arch/x86/minios-x86_32.lds \
-o $D/mini-os-x86_32-c/mini-os
 
gzip -f -5 -c $D/mini-os-x86_32-c/mini-os >$D/mini-os-x86_32-c/mini-os.gz

13. Run the mini-os instance


From the dom0, as root, type


sudo xm create /dev/null kernel=/home/larsr/xenbuild/home/larsr/xen-4.0-testing/stubdom/mini-os-x86_32-c/mini-os.gz name=minios disk=file:/home/larsr/disk2.img,/dev/sda,r vif="" -c