Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Lab01 - Part 1 - Coding assistants

Lab01 - Part 2 - Getting started with xv6

Deliverables due Wed Jan 29th by 6:25pm

  • You will demonstrate your dev environment in lab section on Jan 29th
  • You need to show that you can use the LLM CLI, Aider, compile and run xv6.
  • You need to show that you have added the user programs hello.c and sumargs.c to xv6 and that you can compile and run them.

Part 1 - Development Tools

To assist in developing your code in the class we will get familar with different LLM tools and coding assistants. For this lab you should get the following tools and libraries installed on your local machine: GitHub Access, a Python virtual environment, the LLM CLI, and Aider.

Secure GitHub Access

You will be developing and submitting your code and projects using GitHub. You will need to establish secure shell access to GitHub from your computer. Likely you have done this for other courses. You have two choices: ssh GitHub access or GitHub Personal Access Tokens.

Prompt: “How do I configure ssh access to GitHub for terminal access on macOS?”

Prompt: “How do I configure ssh access to GitHub for terminal access on Windows WSL Ubuntu?”

Prompt: “How do I configure GitHub with Personal Access Tokens to use from the shell?

Python Virtual Environment

I suggest that you create a Python virtual environment to contain and manage all the Python libraries we will be using. I create a global location for all my virtual environments instead of putting them into specific projects.

Prompt: “How do I create and use a Python virtual environment in a shared location such as ~/.venvs?”

OpenRouter.ai Account and API Key

We will use OpenRouter.ai to get access to a wide range of LLMs from frontier models like OpenAI ChatGPT 4o and Anthropic Claude 3.5 Sonnet as well as emerging open weight models like DeepSeek V3. Create an account and add some credits. You can start with a low amount like $5.00 to $10.00. I don’t expect you will need more than $50.00 total for the semester. We will use OpenRouter LLMs for both developing LLM applications as well as for using open source coding assistants.

https://openrouter.ai/

LLM CLI (by Simon Willison)

https://github.com/simonw/llm

You will need to install the OpenRouter.ai plugin:

https://github.com/simonw/llm-openrouter

Aider for AI Pair Programming

https://aider.chat/

I use Aider from the command line, but there are also a VS Code extensions for Aider. I’m not sure which one is the best, so I’d love feedback if you try them. Here is one that appears to be actively worked on:

https://github.com/lee88688/aider-composer

Aider also has a “–watch-files” mode that does not require a extension and can work with any IDE:

https://aider.chat/docs/install/optional.html#add-aider-to-your-ideeditor

To use an OpenRouter model by default with Aider you can set you key as described here:

https://aider.chat/docs/llms/openrouter.html

Also, you can set a global model:

$ cat ~/.aider.conf.yml
model: openrouter/anthropic/claude-3.5-sonnet

Part 2 - Getting started with xv6

Overview

In this class we will be studying, modifying, and extending the xv6 Unix-like operating developed at MIT. Specifically, we will be using the latest RISC-V version of xv6. In this lab you will learn how to build and run xv6 using the Qemu machine emulator. You will also add some user-level programs to xv6. Here are some useful links:

xv6-riscv on GitHub
xv6-riscv-book

The xv6 operating system is a rewrite of Unix Version 6. Like most OS kernels, xv6 is written in C with some Assembly Language. The xv6 kernel and user programs total just under 10,000 lines of code, yet it is a fairly complete kernel supporting common Unix system calls, processes, virtual memory, and a modern file system. Originally, xv6 was written to work on Intel x86 processors, but it was ported a few years ago to RISC-V. RISC-V is a modern processor architecure that is open for public use, like the Linux kernel. At USF we started using RISC-V in CS 315 Computer Architecture in the Fall of 2022. Knowing RISC-V is not required, but is helpful in understanding some of the lower-level pieces of the kernel code. We will review aspects of RISC-V when looking at these parts of the kernel.

In order to compile and run xv6 you will need to have access to a RISC-V cross-compiler toolchain and Qemu.

Prompt: “How do I install risc-v cross compiler tools on macOS using Homebrew?”

Prompt: “How do I install risc-v cross compiler tools on Ubuntu linux?”

Prompt: “How do I install qemu on macOS using Homebrew?”

Prompt: “How do I install qemu on Ubuntu Linux?”

Clone xv6

For labs and projects, I will provide initial starter xv6 code, but for now you can clone your own copy of xv6 to compile and run.

$ cd
$ mkdir test-xv6 
$ cd test-xv6
$ git clone git@github.com:mit-pdos/xv6-riscv.git

Be sure to substitute username with your username.

Compile and Run xv6

Now, you can cd into the xv6-riscv directory and compile and run xv6:

$ cd xv6-riscv
$ make qemu

After lots of files are compiled, you should see:

qemu-system-riscv64 -machine virt -bios none -kernel kernel/kernel -m 128M -smp 3 -nographic -global virtio-mmio.force-legacy=false -drive file=fs.img,if=none,format=raw,id=x0 -device virtio-blk-device,drive=x0,bus=virtio-mmio-bus.0

xv6 kernel is booting

hart 1 starting
hart 2 starting
init: starting sh
$

You can try some basic commands in xv6:

$ ls
.              1 1 1024
..             1 1 1024
README         2 2 2305
cat            2 3 32816
echo           2 4 31696
forktest       2 5 15816
grep           2 6 36256
init           2 7 32168
kill           2 8 31616
ln             2 9 31432
ls             2 10 34760
mkdir          2 11 31672
rm             2 12 31664
sh             2 13 54240
stressfs       2 14 32544
usertests      2 15 180304
grind          2 16 47424
wc             2 17 33744
zombie         2 18 31032
console        3 19 0
$ wc README
49 325 2305 README

To quit xv6 (actually Qemu), type:

CTRL-a x

That is type CTRL-a then x.

On many computer keyboards the CTRL key will be labels control.

And you should see something like:

$ QEMU: Terminated
$

Adding new user programs to xv6 - Manually

Once you have everything set up to compile and run xv6 you can now added new user-level programs to xv6. We will start with hello.c. We will do this both in a manual fashion and with Aider.

Consider hello.c:

#include "kernel/types.h"
#include "kernel/stat.h"
#include "user/user.h"

int
main(int argc, char *argv[])
{
  printf("Hello, xv6!\n");
  exit(0);
}

Add this file to the user directory in the xv6 repo. Notice that this program does not use the typical C header files:

#include <stdio.h>
#include <stdlib.h>

xv6 has a limited C number of C library functions. The prototypes for these functions are found in user/user.h and implemented in user/ulib.c.

Next edit the Makefile in the root directory of the xv6 repo and look for the UPROGS variable:

UPROGS=\
        $U/_cat\
        $U/_echo\
        $U/_forktest\
        $U/_grep\
        $U/_init\
        $U/_kill\
        $U/_ln\
        $U/_ls\
        $U/_mkdir\
        $U/_rm\
        $U/_sh\
        $U/_stressfs\
        $U/_usertests\
        $U/_grind\
        $U/_wc\
        $U/_zombie\

Add $U/_hello\ after $U/_grep\ to keep the list in alphabetical order.

Once you’ve added hello.c and modified the Makefile, you should be able to type make qemu then run hello the xv6 shell prompt.

Adding new user programs to xv6 - Aider

Now try to add the hello program to xv6 using Aider:

$ aider
───────────────────────────────────────────────────────────────────────────────────────────────────
Add .aider* to .gitignore (recommended)? (Y)es/(N)o [Yes]:
Added .aider* to .gitignore
Aider v0.72.2
Main model: openrouter/anthropic/claude-3.5-sonnet with diff edit format, infinite output
Weak model: openrouter/anthropic/claude-3-5-haiku
Git repo: .git with 75 files
Repo-map: using 4096 tokens, auto refresh
───────────────────────────────────────────────────────────────────────────────────────────────────
> Add a new user program to xv6 called "hello". The hello program should print "Hello, xv6!\n"

Add sumargs.c to xv6

Now write a program called sumargs.c that will sum up all of the integer arguments provided on the command line:

$ sumargs 1 2 3 4 5
15

Do this both manually and with Aider.