Linux KVM as a Learning Tool
Listing 5. Linker Script kernel16.lds
OUTPUT_FORMAT(binary)
SECTIONS {
. = 0;
.text : { *(.init) *(.text) }
. = ALIGN(4K);
.data : { *(.data) }
. = ALIGN(16);
.bss : { *(.bss) }
. = ALIGN(4K);
.edata = .;
}
The SECTIONS command controls how to make the mapping and how to place the output sections in memory. Directives follow the syntax:
.output-section : [optional-args]
{ input-section, input-section, ... }
The kernel16.lds script sets the current location at offset 0x0. Then, the output .text section will start there and will contain the contents of any .init and .text input sections.
Next, we align the current location to a 4KB boundary and create the .data and .bss output sections. Use kernel16.lds to generate the kernel image as shown in Listing 6.
Listing 6. Building a 16-Bit Kernel Image
$ gcc -nostdlib -Wl,-T,kernel16.lds kernel1.S -o kernel1 $ ls -oh kernel1 -rwxr-xr-x 1 djprotti 64K 2008-10-17 19:09 kernel1
The -nostdlib flag avoids linking the standard system startup files and libraries (these will not be available inside our virtual machines). After this, we have our 64Kb 16-bit real-address kernel image.
The Makefile in Listing 7 contains the commands to build both the kernel and the launcher.
Listing 7. Makefile
# If KVM was compiled from sources and you have errors about
# missing asm/kvm*.h files, copy them from
# kvm-XX/kernel/include/asm/* to {prefix}/include/asm/
CC=gcc
KERNEL16_CFLAGS=-nostdlib -ffreestanding -Wl,-T,kernel16.lds
all: launcher kernel1
launcher: launcher.o
$(CC) launcher.o /usr/lib/libkvm.a -o launcher
launcher.o:
kernel1: kernel1.S
$(CC) $(KERNEL16_CFLAGS) kernel1.S -o kernel1
clean:
rm *.o launcher kernel1
Launch the virtual machine with kernel1 as guest with the following command:
$ ./launcher kernel1
If everything goes well, you will see no output, and the guest kernel should be consuming all of its available CPU. If you run the top command in another console, and you see output similar to that of Listing 8 (100% CPU usage for the launcher process), you have your kernel running in your first KVM virtual machine!
Listing 8. Output of top While Our Launcher Is Running
PID USER S %CPU %MEM TIME+ COMMAND 8002 djprotti R 100 0.8 1:53.19 launcher 7428 djprotti S 0 0.8 0:04.45 gnome-terminal 8005 djprotti R 0 0.0 0:00.02 top 1 root S 0 0.0 0:03.92 init 2 root S 0 0.0 0:00.00 kthreadd 3 root S 0 0.0 0:00.12 migration/0 4 root S 0 0.0 0:02.76 ksoftirqd/0 5 root S 0 0.0 0:00.01 watchdog/0
Now, let's build a kernel that communicates with the world. First, choose one of the I/O ports and use it to implement a “serial port”. Name the chosen port as IO_PORT_PSEUDO_SERIAL (as shown in Listing 10), then modify the outb callback in the launcher to interpret bytes sent to this port as characters printed to a serial console, and redirect them to launcher's standard output as shown in Listing 9.
Listing 9. Pseudo-Serial Port Implementation in launcher.c
#include "runtime.h"
static int my_outb (void *opaque, uint16_t addr, uint8_t data)
{
if (addr == IO_PORT_PSEUDO_SERIAL)
if (isprint(data) || data == '\n')
putchar(data);
else
putchar('.');
else
printf("outb: %x, %d\n", addr, data);
fflush (NULL);
return 0;
}
Then, build a second kernel (kernel2) whose only task is to print “Hello\n” to its pseudo-serial port and then halt, as shown in Listing 10.
Listing 10. kernel2.S
#include "runtime.h"
.code16
start:
mov $0x48,%al // H
outb %al,$IO_PORT_PSEUDO_SERIAL
mov $0x65,%al // e
outb %al,$IO_PORT_PSEUDO_SERIAL
mov $0x6c,%al // l
outb %al,$IO_PORT_PSEUDO_SERIAL
mov $0x6c,%al // l
outb %al,$IO_PORT_PSEUDO_SERIAL
mov $0x6f,%al // o
outb %al,$IO_PORT_PSEUDO_SERIAL
mov $0x0a,%al // new_line
outb %al,$IO_PORT_PSEUDO_SERIAL
hlt // halt the processor
. = 0xfff0
ljmp $0xf000, $start
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- New Products
- The Pari Package On Linux
- RSS Feeds
- This is the easiest tutorial
5 hours 19 min ago - Ahh, the Koolaid.
10 hours 57 min ago - git-annex assistant
16 hours 57 min ago - direct cable connection
17 hours 19 min ago - Agreed on AirDroid. With my
17 hours 30 min ago - I just learned this
17 hours 34 min ago - enterprise
18 hours 4 min ago - not living upto the mobile revolution
20 hours 55 min ago - Deceptive Advertising and
21 hours 31 min ago - Let\'s declare that you have
21 hours 32 min ago
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




Comments
Exit instead of hlt
Thanks for this excellent article. I've tested out the sample but the program never returns after the kvm_run() call. I guess this is because the vcpu is halted in the last instruction of the test program. But how do I exit the KVM altogether and resume execution from kvm_run() onwards? Is there any documentation for libkvm somewhere I can consult?
This appears to be the libkvm in question, no?
http://www.linux-kvm.org/page/Code
See the userspace git tree. Is this the lib we need to be building to follow this article? I've been looking to do something like this with KVM for a while to create my own forth-like environment without having to screw around with low level hardware (or at least put it off til something interesting already works). Look forward to the follow up article. When is it scheduled?
-- Ben Scherrey
LibKVM on Ubuntu? Nonesuch...
My attempt to follow this tutorial died on page 2, when it announced that all of the examples would be using the LibKVM library. Several hours of searching on Google and I've found nothing; no way to install or use the LibKVM library unless I'm on BSD.
If anyone has a workaround for this, I'd love to hear it. I was really looking forward to following the tutorial.
Different libkvm
That's a different library. You need to install the qemu-kvm-devel package. Note you will probably have to get it directly from sourceforge since it does not appear to be in the Ubuntu repos. I don't use Ubuntu much so maybe I'm overlooking something, however, I know that openSUSE does not have a package for it either (at least in the standard places at 11.0). Make sure that you get the version that corresponds to the version of kvm that you have installed. For example, here is a link to the -devel package for release 88.
If you have to install it from sourceforge some fiddling around will probably be required. First, you'll have to build it, then install it, and then potentially modify your include/library paths to find the needed items.
Mitch Frazier is an Associate Editor for Linux Journal.
I used REHL5.5 installed devel package for release 88
I used REHL5.5 installed devel package for release 88, but still cannot find libkvm.h and libkvm.a, the installation was successful, can you give me some suggestion? thanks.