Friday, January 4, 2013

Install Simplescalar 3.0 on Ubuntu 12.04

My platform:
Intel® Core™ i5-3550 CPU
32bit Ubuntu 12.04

Here is the procedure for installing simplescalar on my computer, pay attention that this is written for newbies to linux therefore it may contain something that may not be so obvious to them. For those who does not need to use the cross-compiler and binutils,  step 5 alone is enough to setup simplescalar and run some binary form benchmarks.


1. Download the sources


Download simplesim-3v0e.tgz and simpletools-2v0.tgz from http://www.simplescalar.com/index.html

Download simpleutils-990811.tar.gz (If you insist on using the simpleutils-2v0.tgz on the official page of simplescalar, stop here and try this installation guide instead)
Download Cross Compiler gcc-2.7.2.3
You man also need the benchmark.

2. Setup the environment


i.

export IDIR=/any/directory/you/choose/ (for example, mine is /home/tangkk/Build/simplescalar)
export HOST=i686-pc-linux
export TARGET=sslittle-na-sstrix
(Assuming your host is also little endian. It could be ssbig-na-sstrix. If this is the case, all the following appeared "sslittle-na-sstrix" should be replaced by "ssbig-na-sstrix".)

(Pay attention that if you ever quit the terminal, next time you loggin to the terminal you need to assign these values again. To give you some idea of what these setting may be, for the HOST part, i686 specifies your target CPU's architecture, other possible value maybe "arm", "mips", "i386", etc., and linux specifies your operating system, other possible value could be "solaris", "gnu"..., the "pc" part specifies the "company"; for the TARGET part, it specifies the endian. You can check out more information at [1])


(Further reading: Host and TargetConfiguration Names, Build,Host and Target)

ii.

make the IDIR directory by executing "mkdir $IDIR" and then move all the tarballs you've just downloaded into this directory.

iii.

make sure you update your softwares:

sudo apt-get update


make sure you install:

build-essential
flex
bison
gcc-multilib(may be optional)
g++-multilib(may be optional)

If you haven't, do:


sudo apt-get install <package name>


3. Unpack the Simpletools-2v0

(Basically this package contains the sources of a gcc(GNU C Compiler) and a glibc ported to simplescalar architecture. And according to [7], building the glibc is non-trivial, thus this Simpletools package contains pre-compiled libraries in both ssbig-na-sstrix/ and sslittle-na-sstrix/ folder)

Simply do:


cd $IDIR

tar xvfz simpletools-2v0.tgz
rm -rf gcc-2.6.3

There is a gcc-2.6.3 folder, but you can remove it because later we will use the newer version gcc-2.7.2.3(also a cross-compiler) instead.


(after this step you got ssbig-na-sstrix and sslittle-na-sstrix folders under the $IDIR, and they both contains an include folder and a lib folder)

4. Install the SimpleUtils-990811

(Basically this package contains the sources of a cross GUN binutils)

First we unpack the tarball:


cd $IDIR

tar xvfz simpleutils-990811.tar.gz
cd simpleutils-990811

Then, replace the "yy_current_buffer" by "YY_CURRENT_BUFFER" in the file /ld/ldlex.l .(You can to use "vim" or "gedit" to fix it.)


After that, you can configure:


cd $IDIR/simpleutils-990811

./configure --host=$HOST --target=$TARGET --with-gnu-as --with-gnu-ld --prefix=$IDIR
(If you want to know the meaning of these settings, refer to the [1]. Here because we are to build a "cross" GNU binutils, both HOST and TARGET should be specified)

(It should be successful configured. If not, check whether you follow all the above instructions before you ask someone. This is also strongly suggested for all the following steps.)


Then you can make and install with:


make

make install

(after this step you got another 5 folders under $IDIR, they are: bin, lib, include, man, share. And moreover, a bin folder is also added to both the ssbig-na-sstrix and sslittle-na-sstrix folder)

5. Install the Simplesim-3v0e

(This package contains the sources of the simplescalar architecture and micro-architecture(or we can say sources of the simplescalar functional simulator and performance simulator, which is basically referring to the same thing)

Firstly, unpack it:


cd $IDIR

tar xvfz simplesim-3v0e.tgz
cd simplesim-3.0
make config-pisa
make

(or make config-alpha according to your need
or if you want to change the target configuration from
one to another, you need to:
make clean
make config-alpha)


(Because the simulator runs on the hosts only, it is a native build. Everything should be OK and afterwards you will see "my work is done here...")


Run the following to test the simulator

./sim-safe tests/bin.little/test-math

(You will see something like 

sim: ** starting functional simulation **
pow(12.0, 2.0) == 144.000000
pow(10.0, 3.0) == 1000.000000
pow(10.0, -3.0) == 0.001000
...)

6. Install the gcc cross compiler

This is the most error-prone part for the whole installation.

As always, unpack the file first:


cd $IDIR

tar xvfz gcc-2.7.2.3.ss.tar.gz
cd gcc-2.7.2.3
./configure --host=$HOST --target=$TARGET --with-gnu-as --with-gnu-ld --prefix=$IDIR

so far so good! But we cannot proceed with a "make" here because there will be many errors because of various incompatibilities. To get over these, we need to fix some of the sources:


Firstly, for our convenience, do the following to get ourselves with write access to the current directory

chmod -R +w .
(Don't forget about the little "." at the end of this command!)

Following the instructions provided by [2], we have:

i.
Appending "-I/usr/include" at the end of the 130 line of the Makefile

ii.

Replace <varargs.h> with <stdarg.h> in the 60 line of protoize.c

iii.

In the 341 line of obstack.h, change
*((void **)__o->next_free)++   to be *((void **)__o->next_free++)

iv.

Run:

cp ./patched/sys/cdefs.h ../sslittle-na-sstrix/include/sys/cdefs.h 

cp ../sslittle-na-sstrix/lib/libc.a ../lib/
cp ../sslittle-na-sstrix/lib/crt0.o ../lib/

v.

Then build it[3].

make LANGUAGES="c c++" CFLAGS="-O" CC="gcc"

Then you will come across with errors regarding to insn-output.c, so we fix it by appending "\" at the end of the 675, 750 and 823 line of the file insn-output.c. (Note that if you "make clean" and then "make" again, you should do this once again because this file is generated by the build procedure.) Then make again by:

make LANGUAGES="c c++" CFLAGS="-O" CC="gcc"

Then you will come across something like 
"*** buffer overflow detected ***: /home/tangkk/simplescalar/sslittle-na-sstrix/bin/ar terminated"
This could be solved by methods suggested by [2], which is quoted here:
"2 At this point you might encounter a “buffer overflow” if you use Ubuntu 8.10 or later. If so,
download the following files and put them in $IDIR/sslittle-na-sstrix/bin:
(And I should add one more comment here for the newcomers:
cd $IDIR/sslittle-na-sstrix/bin/
chmod +x ar ranlib)

Now go back to your gcc-2.7.2.3 directory, after doing so and you can make once again

make LANGUAGES="c c++" CFLAGS="-O" CC="gcc"

Then you may come across something like
"In file included from /usr/include/features.h:389,
                 from /usr/include/limits.h:27,
                 from include/limits.h:112,
                 from include/syslimits.h:7,
                 from include/limits.h:11,
                 from ./libgcc2.c:1121:
/usr/include/gnu/stubs.h:7: gnu/stubs-32.h: No such file or directory"

This maybe a bug in ubuntu but I don't know for sure. Refering to [4], it can be solved by appending "-I/usr/include/i386-linux-gnu" at the end of the line 130 of Makefile. (If it still doesn't work, install  gcc-multilib and g++-multilib and try again)

(So now the 130 line of Makefile becomes:
GCC_CFLAGS=$(INTERNAL_CFLAGS) $(X_CFLAGS) $(T_CFLAGS) $(CFLAGS) -I./include -I/usr/include -I/usr/include/i386-linux-gnu
)

Then you make again:

make LANGUAGES="c c++" CFLAGS="-O" CC="gcc"

And still there is error regarding to the generated file cxxmain.c. It can be solved by the method suggested by [5], namely, simply commenting out the 2978‐2979 line of cxxmain.c. Then make again.

make LANGUAGES="c c++" CFLAGS="-O" CC="gcc"

There should be no error this time.

7. Make enquire
Also under the "$IDIR/simplescalar/gcc-2.7.2.3" directory, do

make enquire

Then it will pop out some error as: "undefined reference to `__isoc99_sscanf", which, referring to [6], could be solved by adding "-D_GNU_SOURCE" after the "$(ENQUIRE_CFLAGS)" in the line 995 of the Makefile under the directory (namely $IDIR/simplescalar/gcc-2.7.2.3). Then make again

make enquire

Finally we are done!

Finally we follow the rest of those old references to install the cross compiler:

../simplesim-3.0/sim-safe ./enquire -f > float.h-cross
make LANGUAGES="c c++" CFLAGS="-O" CC="gcc" install

8. Hello World!
We can check it with a hello.c program. Use vi to write the following program called hello.c:

#include <stdio.h>
main() {
  printf("Hello World!\n");
}

Then compile it with the cross compiler:

$IDIR/bin/sslittle-na-sstrix-gcc –o hello hello.c
(If you fail to run this, try entering the above "bin" directory and run "./sslittle-na-sstrix-gcc -o ../hello ../hello.c" assuming that the hello.c program is within the $IDIR directory. But if you still fail, maybe there is some chaos with your environment viable, restart the terminal and try it once again)

The you can run the output file "hello" with the simulator:

$IDIR/simplesim-3.0/sim-safe hello

If you see something like this:

sim: ** starting functional simulation **
Hello World!

Congratulations!

For testbenches: the tests-alpha/ and tests-pisa/ folders under simplesim-3.0/ contains some benchmarks, also there are some instructor's benchmarks here.




To remind once again, if you get stuck in any of these steps, check whether you strictly follow all the previous steps first before you ask someone else.


references:

[1] http://www.simplescalar.com/docs/install_guide_v2.txt
[2] http://www.ann.ece.ufl.edu/courses/eel5764_10fal/project/1.36445-Simplescalar-installation-instructions.pdf
[3] http://www.ann.ece.ufl.edu/courses/eel5764_10fal/project/simplescalar_installation.pdf
[4] http://gcc.gnu.org/ml/gcc/2012-02/msg00314.html
[5] http://harryscode.blogspot.hk/2008/10/installing-simplescalar.html
[6] http://lists.gnu.org/archive/html/ltib/2011-11/msg00009.html
[7] A User’s and Hacker’s Guide to the SimpleScalar Architectural Research Tool Set (for tool set release 2.0), Todd M. Austin, 1997

Also thanks for the page http://www.ann.ece.ufl.edu/courses/eel5764_10fal/project.html,  although I did not reference it! It contains a lot of useful links.

The following link is a very good Lab on how to use simplescalar:
http://www.ecs.umass.edu/ece/koren/architecture/Simplescalar/
whose parent link:
http://www.ecs.umass.edu/ece/koren/architecture/
contains a lot of useful tools on understanding computer architecture and organization as well.

tangkk