All homework submissions are to be made via Git. You must submit a detailed list of references as part your homework submission indicating clearly what sources you referenced for each homework problem. You do not need to cite the course textbooks and instructional staff. All other sources must be cited. Please edit and include this file in the top-level directory of your homework submission. Homeworks submitted without this file will not be graded.
Before you begin the programming for this assignment, you must first do two things: get access to GitHub and setup a VMware virtual machine (VM) that you will use for your development work.
You will be using Git via GitHub for course submissions for the class. Please make sure you sign up for a GitHub account if you do not yet have one, and follow the instructions for the W4118 GitHub organization, including filling out the Google Form listed there so that we can associate your GitHub username with your Columbia UNI. If you do not complete the form prior to the homework deadline, you will receive an automatic zero for the assignment.
Once you have a GitHub account and login, you can create your GitHub repository for this assignment. The GitHub repository you will use can be cloned using git clone git@github.com:W4118/f23-hmwk1-UserName.git (Replace UserName with your own GitHub username). Be aware that commits pushed after the deadline will not be considered. Refer to the homework policy section on the class web site for further details.
You will be using a VM that you will setup for all homework assignments. Follow the instructions provided to setup a VM using the Debian Linux distribution we will use for this class. Using a VM ensures that you have a consistent development and testing environment with what we will be using to grade your assignments. For subsequent homework assignments, using a VM for testing will also ensure that a kernel you boot does not mess up your actual computer.
For all programming problems you will be required to submit source code, a Makefile, a README file documenting your files and code, and a test run of your programs. The README should explain any way in which your solution differs from what was assigned, and any assumptions you made. For this assignment, you will have a separate subdirectory for each part of the assignment, and each subdirectory should contain its own Makefile and source code. You must provide a Makefile for each part of this assignment. The README should be placed in the top level directory of your GitHub repository for this assignment. Refer to the homework submission page on the class web site for additional submission instructions. In addition, please pay attention to the additional requirements listed at the bottom of this assignment.
An operating system like Linux makes it easy to run programs. For example, from a shell, it is easy to write, compile, and run a simple hello world C program:
$vi hello.c #include <stdio.h> int main() { printf("hello, world\n"); } $gcc hello.c -o hello $./hello hello, worldThe operating system makes this easy by providing various functions to enable the program to perform I/O such as printing, and the shell to execute the program in response to typing the program executable name at the shell prompt. The shell itself is just another program. For example, the Bash shell is an executable named bash that is usually located in the /bin directory. So, /bin/bash.
Try running /bin/bash or just bash on a Linux (or BSD-based, such as Mac OS X) operating system's command line, and you'll likely discover that it will successfully run just like any other program. Type exit to end your shell session and return to your usual shell. (If your system doesn't have Bash, try running sh instead.) When you log into a computer, this is essentially what happens: Bash is executed. The only special thing about logging in is that a special entry in /etc/passwd determines what shell runs at log in time.
Write a simple shell in C. The requirements are as follows.
$/bin/ls -lha /home/w4118/my_docs
"error: %s\n", strerror(errno) OR "error: %s\n", "your error message"
So for example, you would likely use: fprintf(stderr, "error: %s\n", strerror(errno));
Typically, a system call will return -1 in the case of an error (malloc will return NULL). If a function call sets the errno variable (see the function's man page to find out if it does), you should use the first error message as described above. As far as system calls are concerned, you will want to use one of the mechanisms described in Reporting Errors or Error Reporting.
The simple shell you wrote in Part 1 calls various system calls such as fork(), but also relies on various C library functions that in turn call other system calls. You can use strace to see what system calls are being called when you run simple shell. First, install strace:
sudo apt install straceThen you can run strace with simple shell:
strace -o trace.txt w4118_sh
which will dump the system calls executed into the file trace.txt. For example, if you used printf() to output text in simple shell, you will find that it in turn calls a system call to actually perform the I/O operation because I/O is controlled by the operating system. C library functions such as printf() are technically not part of the C language, but made possible by relying on functionality provided by the operating system.
To gain a better understanding of how C library functions rely on operating system functionality, modify your simple shell so that it does not call any C library functions that call other system calls. Instead, your simple shell should directly call any system calls that it implicitly uses. For example, your simple shell should not call printf() but instead call write() on STDOUT. Other C library functions that you may also have to replace include getline(), malloc(), etc. You do not have to be overly concerned with efficiency, so you may find it easier to use mmap() instead of sbrk() for any dynamic memory allocation you need to do. For example, you may find it helpful to see this implementation of malloc(). String manipulation functions such as strtok and strcmp do not call system calls and do not need to be replaced.
Note that your implementation of the various functions only has to work specifically for your simple shell. For example, you do not need to implement all functionality supported by printf(), only what functionality is required to print the output that your shell generates. Similarly, your input functionality only needs to work for any ascii characters generated from a keyboard.
Your shell executable should be named w4118_sh2. Your shell source code should be mainly in shell2.c, but you are free to add additional source code files as long as your Makefile works, and compiles and generates an executable named w4118_sh2 in the same top level directory as the Makefile. If we cannot simply run make and then w4118_sh2, you will be heavily penalized. w4118_sh2 should have all the same functionality as w4118_sh, except that it does not call any C library functions that call other system calls.
Without an operating system, running a program on a computer will be harder. When the power button is pressed, the CPU is reset to its initial state and firmware is executed. The firmware checks the hardware resources of the computer, loads the first program on the storage device (for example, the hard drive) into the RAM and transfers control to the program.
In older systems, the BIOS (Basic Input Output System) is the firmware that checks hardware, then transfers control over to the first program. Newer systems now use a different firmware standard known as UEFI (Unified Extensible Firmware Interface), which provides small abstractions on top of the hardware to make it easier for operating system and driver developers to work with different kinds of hardware. UEFI looks for a program at an architecture-specific path on the storage device, and runs it on boot. For x86_64 machines, this first program is located at /EFI/BOOT/BOOTX64.EFI, whereas on aarch64 machines this program is located at /EFI/BOOT/BOOTAA64.EFI. Unlike Linux filesystems, in UEFI these initial filesystem paths are case insensitive.
Usually, the bootloader is located at these architecture-specific paths, and when run, loads the operating system, which can implement more complex functionality. But it is not necessary. A simple operating system can also be loaded directly by UEFI. The simple operating system can be as simple as a program that prints "hello, world" to the screen. However, the program that is loaded by UEFI does not, at least initially, have an easy-to-use C environment in which to execute.
typedef struct { UINT8 Blue; UINT8 Green; UINT8 Red; UINT8 Reserved; } EFI_GRAPHICS_OUTPUT_BLT_PIXEL;Pixels in physical memory are not automatically drawn to screen. We have provided a call to GOP's Blt function, which will copy pixels to video hardware. The UEFI specification on GOP may be a useful reference to understand how this function works. Once you have finished drawing pixels to the screen, you need to prevent your program from exiting. In particular, the provided main.c does nothing further after efi_main exits, and control is transferred back to the UEFI firmware. As a result, UEFI will clear the screen and try to boot from elsewhere in the system, or open the UEFI diagnostics screen. You should ensure that your hello world OS does not let the machine do this and keep the pixels you have drawn. A loop of some kind here would be useful. A good resource for understanding the provided UEFI setup code, as well as other graphics manipulation functions, is the UEFI specification document. The detailed reader may note that UEFI provides other convenience functions to print 16-bit character strings to the console, using the Simple Text Output Protocol. It is optional, but you may find these functions to be useful while debugging; however, you may not use these functions in your final submission. Only the functions under GOP should be used.
sudo apt install clang lld mtoolsx86 VM: If you are using an x86 VM, the commands you need to execute are:
# compilation clang -target x86_64-unknown-windows -ffreestanding -fshort-wchar -mno-red-zone -c main.c clang -target x86_64-unknown-windows -nostdlib -Wl,-entry:efi_main -Wl,-subsystem:efi_application -fuse-ld=lld-link -o BOOTX64.EFI main.o # disk image creation dd if=/dev/zero of=disk.img bs=1k count=1440 mformat -i disk.img -f 1440 :: mmd -i disk.img ::/EFI mmd -i disk.img ::/EFI/BOOT # copy program into the disk mcopy -i disk.img BOOTX64.EFI ::/EFI/BOOTArm VM: If you are using an Arm VM, the commands you need to execute are:
# compilation clang -target aarch64-unknown-windows -ffreestanding -fshort-wchar -mno-red-zone -c main.c clang -target aarch64-unknown-windows -nostdlib -Wl,-entry:efi_main -Wl,-subsystem:efi_application -fuse-ld=lld-link -o BOOTAA64.EFI main.o # disk image creation dd if=/dev/zero of=disk.img bs=1k count=1440 mformat -i disk.img -f 1440 :: mmd -i disk.img ::/EFI mmd -i disk.img ::/EFI/BOOT # copy program into the disk mcopy -i disk.img BOOTAA64.EFI ::/EFI/BOOTNote that the instructions are nearly identical for x86_64 and aarch64 (Arm), except for the target architecture and the file name for the UEFI program. Compiling against a Windows target is required, since UEFI uses the Windows Portable Executable file format. A Makefile has been provided to help make this compilation and image creation process easier. The Makefile will attempt to detect your machine's architecture, which you can override by running make ARCH=aarch64 or make ARCH=x86_64.
cd "$(git rev-parse --show-toplevel)/part3" && touch .armpls && git add .armpls && git commit -m "Arm pls"We will then run make ARCH=aarch64 and boot your program on an aarch64 host.