This homework is due on Thursday, September 13, at 11:59:59 PM
(Eastern daylight time). You must use submit to
turn in your homework like so:
submit cs421_jtang hw1 greeting.out 0001-your_commit_message.patch platform-driver.txt hw1.c
All of your homework assignments must run under 64-bit Linux, specifically Ubuntu 18.04 LTS ("Bionic Beaver"), and all projects will involve making changes to the Linux kernel. In addition, all later assignments require that you have a working Linux virtual machine. The projects require that you have finished customizing your Linux kernel. In this homework, you will become familiar with the procedures of installing and updating the kernel inside a VM.
If you are unable to customize your kernel for this homework, you will get a zero on the later projects.
This homework is divided into several parts that will help you perform the following objectives:
If you choose to work in the lab, you will need an external USB hard drive or flash drive for development as well as for backups. You will need to have at least 40 GiB of free space on the drive to store your VM image on it, so a 64 GiB or larger drive is highly recommended. Also, you will need to format the drive to filesystems that supports files of size > 4 GiB. Many flash drives come pre-formatted as FAT32, which do not support file sizes >= 4 GiB. FAT32 will not work for this reason! You should format your drive as either NTFS (if you will be using Windows with the drive as well) or a Linux filesystem such as ext4.
In order to aid development of assignments in this course, we will be running the submissions under virtualization. Virtualization allows us to run a virtual machine on an actual physical machine. In this way, we can run a second guest operating system inside the regular host operating system. To do the assignments in this class, we assume you will have access to a relatively modern PC that can run VirtualBox. VirtualBox requires an x86/64 CPU with a decent amount of RAM, at least 4 GiB. In addition, your host CPU should support the x86 virtualization extensions (VT-x for Intel processors, or AMD-V for AMD processors). For more information about hardware and software requirements for VirtualBox, please consult the VirtualBox website. All assignments are expected to run in 64-bit mode on VirtualBox.
VirtualBox is available on the machines in the ITE 240 lab under both Linux and Windows. You can also download it for free from https://www.virtualbox.org/wiki/Downloads to run under your own Windows, Mac, or even Linux host operating system. This assignment was tested using VirtualBox version 5.2.16.
For the purposes of assignments in this course, we will be using the x86 (64-bit) version of the Ubuntu 18.04 Linux distribution. In addition, the custom kernels that are built in this class will be based on the Linux kernel version 4.17 (the latest stable kernel).
Perform the following tasks to create the environment that will be used to complete assignments in this course. These instructions are based upon a WikiHow article.
sudo apt-get update && sudo apt-get upgradeYou will need to enter your password to run the operation as root.
sudo apt-get install bison flex gcc g++ make libelf-dev libqt4-dev libncurses5-dev libssl-dev perl pkg-config sudo apt-get install emacs vim git indent
Be aware you may need to reinstall the Guest Additions each time you update the kernel for this class.
Many of the commands that you will be running within the VM will require root privileges. There are a variety of methods to elevate your user privileges on Linux. You can use any of the following methods to do so:
sudo -s (enter your user password when prompted) (perform any commands to execute as root) exitOR
sudo sh (enter your user password when prompted) (perform any commands to execute as root) exitOR
sudo (command to execute as root)
The instructions in this and all future assignments will explicitly specify when sudo is needed. If it is not needed, do not use the command. Arbitrarily using sudo will not magically fix any issues.
Ubuntu 18.04 ships with a 4.15 Linux kernel. As that this class involves learning how operating systems really work, you will upgrade the kernel to a newer version. Follow these steps to obtain the Linux kernel source files:
git clone git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable.git linuxThis last command fetches the Linux source code repository, via git, and writes the files into the new linux directory. If you are not familiar with the git version control system, you should work through the Interactive Git Tutorial before proceeding. If you have already cloned the repository, delete the old repository prior to cloning again.
cd linux git checkout linux-4.17.yFor the rest of this assignment, the sources in the linux directory will be referred to as your working copy of the kernel, or alternatively, Linux kernel repository. If you ever need the original unmodified code, re-run the clone command in a different directory; advanced git users may also want to create their own branches within the repository.
git config ‐‐global user.email "gburdell@umbc.edu" git config ‐‐global user.name "George Burdell"(Note the above is dash, dash, "global". You will need to type the commands by hand, instead of copying and pasting it.)
wget https://www.csee.umbc.edu/~jtang/cs421.f18/homework/hw1/0001-x86-irq-Allow-for-software-triggered-IRQ.patch wget https://www.csee.umbc.edu/~jtang/cs421.f18/homework/hw1/0002-x86-Create-kernel-defconfig-for-CS421-VirtualBox-VMs.patch
git am 0001-x86-irq-Allow-for-software-triggered-IRQ.patch git am 0002-x86-Create-kernel-defconfig-for-CS421-VirtualBox-VMs.patch
To repeat, none of the commands in this section are to be run under sudo. Do not use sudo when running any commands in this section, otherwise, you will need to restart Part 2.
Next, you will perform a simple modification of the official Linux kernel. At startup, your custom kernel will write a message to the kernel log using your name. If you have followed all of the instructions up to this point, you should have no trouble doing this.
dmesg
dmesg | head
[ 0.000000] Linux version 4.17.17+ (tang@CMSC421) (gcc version 7.3.0 (Ubuntu 7.3.0-16ubuntu3)) #2 SMP Sat Aug 18 18:50:52 EDT 2018 [ 0.000000] This is jtang@umbc.edu kernel! [ 0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-4.17.17+ root=UUID=0b48296a-f662-44fb-a8d3-ffe22a81b6d5 ro quiet splashEmphasis added. Change your kernel to display your UMBC email address.
printk()
function works.
To begin, you need to compile the kernel and accompanying modules as is.
make mrproper make cs421_defconfig make clean
make -j3 sudo make modules_install install
GRUB_TIMEOUT_STYLE
from hidden
to menu
. Save and quit, then
run update-grub as uname -a
mkdir -p $HOME/hw1 dmesg | head > $HOME/hw1/greeting.out
Only steps 2 and 5 in this part requires sudo. To reiterate a third time, do not use sudo when running any other command. Otherwise, you will need to restart at Part 2.
Your next objective is to learn about patches and diffs. Since many modifications to the kernel are small (compared to the total kernel source size), such updates are usually distributed in the form of a patch file; those patch files describe differences between the original software and the modified code. If you make minor modifications to the kernel (such as for CS421 projects) you will want to create patches containing the diffs. Later projects may distribute minor kernel changes in the form of patches.
#
will be ignored. Save
and exit.
git format-patch HEAD^1 -o $HOME/hw1
For a fourth time, none of the commands in this section requires sudo. Doing so will result in your git repository being in an inconsistent state, and you will need to restart at Part 2.
Now that you have a working Linux virtual machine, it is time for some programming. View the two virtual files /proc/ioports and /proc/iomem:
cat /proc/ioports cat /proc/iomemI/O Ports are an archaic system on x86 processors to communicate with peripherals, while I/O Mem is used for modern for memory-mapped devices. In Linux, non-privilege access to these ports only show a device's existence, and not the addresses themselves. Thus, try running the above cat commands with and without sudo.
Write a program, in C, that takes a single command line argument, a hexadecimal address. Open both files ioports and iomem. Display all lines whose address range encompasses the passed in value. If no lines contain the device address, display a message.
If no device is given, or if the user gives multiple arguments, or if a non-hexadecimal value is given, then display an error message and quit.
SPECIAL RESTRICTION: As that this is an operating
system class, you will learn how the computer actually prints
things. For this assignment, you MAY NOT use the
function printf()
anywhere within your program. You
must find an alternative to displaying strings.
Your program must be called hw1.c, and it will be compiled on Ubuntu 18.04 as follows:
gcc ‐‐std=c99 ‐Wall ‐O2 ‐o hw1 hw1.c(Note the above is dash, dash, "std=c99", and the other flags likewise are preceded by dashes.) There must not be any compilation warnings in your submission; warnings will result in grading penalties. In addition, each code file must be properly indented and have a file header comment, as described on the coding conventions page.
Here is a sample output when run in the VM. Your values may differ. The user need not precede the given address with 0x.
$ ./hw1 1000 ioports: No devices found. iomem: No devices found. $ sudo ./hw1 0170 ioports: 0000-0cf7 : PCI Bus 0000:00 0170-0177 : 0000:00:01.1 0170-0177 : ata_piix iomem: 00000000-00000fff : Reserved $ sudo ./hw1 0xd00 ioports: 0d00-ffff : PCI Bus 0000:00 iomem: 00000000-00000fff : Reserved $ sudo ./hw1 fee00123 ioports: No devices found. iomem: fee00000-fee00fff : Local APIC fee00000-fee00fff : Reserved $ sudo ./hw1 1000 ioports: 0d00-ffff : PCI Bus 0000:00 iomem: 00001000-0009fbff : System RAM $ ./hw1 No address given. $ ./hw1 abcd 1234 Multiple addresses given. $ ./hw1 abcdefg Invalid target address.
strtoul()
may be useful for Part 5.
Sorry, there is no extra credit available for this assignment.