Change example launcher to use unsigned long not u32

Apply Clue 2x4 to lguest userland<->kernel handling code and the
lguest launcher. Pointers are not to be passed in u32's!

Basic rule of thumb: Anything passing u32's back and forth should be
passing unsigned longs to be portable to 64 bit archs.

For those who forgotten already, I repeat: NO POINTERS IN u32!

Signed-off-by: Jes Sorensen <jes@sgi.com>
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Jes Sorensen 2007-10-22 11:03:31 +10:00 committed by Rusty Russell
parent b410e7b149
commit 511801dc31
2 changed files with 30 additions and 28 deletions

View File

@ -473,9 +473,9 @@ static unsigned long setup_pagetables(unsigned long mem,
unsigned long initrd_size, unsigned long initrd_size,
unsigned long page_offset) unsigned long page_offset)
{ {
u32 *pgdir, *linear; unsigned long *pgdir, *linear;
unsigned int mapped_pages, i, linear_pages; unsigned int mapped_pages, i, linear_pages;
unsigned int ptes_per_page = getpagesize()/sizeof(u32); unsigned int ptes_per_page = getpagesize()/sizeof(void *);
/* Ideally we map all physical memory starting at page_offset. /* Ideally we map all physical memory starting at page_offset.
* However, if page_offset is 0xC0000000 we can only map 1G of physical * However, if page_offset is 0xC0000000 we can only map 1G of physical
@ -505,7 +505,7 @@ static unsigned long setup_pagetables(unsigned long mem,
* continue from there. */ * continue from there. */
for (i = 0; i < mapped_pages; i += ptes_per_page) { for (i = 0; i < mapped_pages; i += ptes_per_page) {
pgdir[(i + page_offset/getpagesize())/ptes_per_page] pgdir[(i + page_offset/getpagesize())/ptes_per_page]
= ((to_guest_phys(linear) + i*sizeof(u32)) = ((to_guest_phys(linear) + i*sizeof(void *))
| PAGE_PRESENT); | PAGE_PRESENT);
} }
@ -537,12 +537,13 @@ static void concat(char *dst, char *args[])
* the base of guest "physical" memory, the top physical page to allow, the * the base of guest "physical" memory, the top physical page to allow, the
* top level pagetable, the entry point and the page_offset constant for the * top level pagetable, the entry point and the page_offset constant for the
* Guest. */ * Guest. */
static int tell_kernel(u32 pgdir, u32 start, u32 page_offset) static int tell_kernel(unsigned long pgdir, unsigned long start,
unsigned long page_offset)
{ {
u32 args[] = { LHREQ_INITIALIZE, unsigned long args[] = { LHREQ_INITIALIZE,
(unsigned long)guest_base, (unsigned long)guest_base,
guest_limit / getpagesize(), guest_limit / getpagesize(),
pgdir, start, page_offset }; pgdir, start, page_offset };
int fd; int fd;
verbose("Guest: %p - %p (%#lx)\n", verbose("Guest: %p - %p (%#lx)\n",
@ -586,7 +587,7 @@ static void wake_parent(int pipefd, int lguest_fd, struct device_list *devices)
for (;;) { for (;;) {
fd_set rfds = devices->infds; fd_set rfds = devices->infds;
u32 args[] = { LHREQ_BREAK, 1 }; unsigned long args[] = { LHREQ_BREAK, 1 };
/* Wait until input is ready from one of the devices. */ /* Wait until input is ready from one of the devices. */
select(devices->max_infd+1, &rfds, NULL, NULL, NULL); select(devices->max_infd+1, &rfds, NULL, NULL, NULL);
@ -684,7 +685,7 @@ static u32 *dma2iov(unsigned long dma, struct iovec iov[], unsigned *num)
static u32 *get_dma_buffer(int fd, void *key, static u32 *get_dma_buffer(int fd, void *key,
struct iovec iov[], unsigned int *num, u32 *irq) struct iovec iov[], unsigned int *num, u32 *irq)
{ {
u32 buf[] = { LHREQ_GETDMA, to_guest_phys(key) }; unsigned long buf[] = { LHREQ_GETDMA, to_guest_phys(key) };
unsigned long udma; unsigned long udma;
u32 *res; u32 *res;
@ -705,7 +706,7 @@ static u32 *get_dma_buffer(int fd, void *key,
/* This is a convenient routine to send the Guest an interrupt. */ /* This is a convenient routine to send the Guest an interrupt. */
static void trigger_irq(int fd, u32 irq) static void trigger_irq(int fd, u32 irq)
{ {
u32 buf[] = { LHREQ_IRQ, irq }; unsigned long buf[] = { LHREQ_IRQ, irq };
if (write(fd, buf, sizeof(buf)) != 0) if (write(fd, buf, sizeof(buf)) != 0)
err(1, "Triggering irq %i", irq); err(1, "Triggering irq %i", irq);
} }
@ -787,7 +788,7 @@ static bool handle_console_input(int fd, struct device *dev)
struct timeval now; struct timeval now;
gettimeofday(&now, NULL); gettimeofday(&now, NULL);
if (now.tv_sec <= abort->start.tv_sec+1) { if (now.tv_sec <= abort->start.tv_sec+1) {
u32 args[] = { LHREQ_BREAK, 0 }; unsigned long args[] = { LHREQ_BREAK, 0 };
/* Close the fd so Waker will know it has to /* Close the fd so Waker will know it has to
* exit. */ * exit. */
close(waker_fd); close(waker_fd);
@ -1365,7 +1366,7 @@ static void __attribute__((noreturn))
run_guest(int lguest_fd, struct device_list *device_list) run_guest(int lguest_fd, struct device_list *device_list)
{ {
for (;;) { for (;;) {
u32 args[] = { LHREQ_BREAK, 0 }; unsigned long args[] = { LHREQ_BREAK, 0 };
unsigned long arr[2]; unsigned long arr[2];
int readval; int readval;

View File

@ -43,7 +43,7 @@ static void setup_regs(struct lguest_regs *regs, unsigned long start)
/*L:310 To send DMA into the Guest, the Launcher needs to be able to ask for a /*L:310 To send DMA into the Guest, the Launcher needs to be able to ask for a
* DMA buffer. This is done by writing LHREQ_GETDMA and the key to * DMA buffer. This is done by writing LHREQ_GETDMA and the key to
* /dev/lguest. */ * /dev/lguest. */
static long user_get_dma(struct lguest *lg, const u32 __user *input) static long user_get_dma(struct lguest *lg, const unsigned long __user *input)
{ {
unsigned long key, udma, irq; unsigned long key, udma, irq;
@ -67,7 +67,7 @@ static long user_get_dma(struct lguest *lg, const u32 __user *input)
/*L:315 To force the Guest to stop running and return to the Launcher, the /*L:315 To force the Guest to stop running and return to the Launcher, the
* Waker sets writes LHREQ_BREAK and the value "1" to /dev/lguest. The * Waker sets writes LHREQ_BREAK and the value "1" to /dev/lguest. The
* Launcher then writes LHREQ_BREAK and "0" to release the Waker. */ * Launcher then writes LHREQ_BREAK and "0" to release the Waker. */
static int break_guest_out(struct lguest *lg, const u32 __user *input) static int break_guest_out(struct lguest *lg, const unsigned long __user *input)
{ {
unsigned long on; unsigned long on;
@ -90,9 +90,9 @@ static int break_guest_out(struct lguest *lg, const u32 __user *input)
/*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt /*L:050 Sending an interrupt is done by writing LHREQ_IRQ and an interrupt
* number to /dev/lguest. */ * number to /dev/lguest. */
static int user_send_irq(struct lguest *lg, const u32 __user *input) static int user_send_irq(struct lguest *lg, const unsigned long __user *input)
{ {
u32 irq; unsigned long irq;
if (get_user(irq, input) != 0) if (get_user(irq, input) != 0)
return -EFAULT; return -EFAULT;
@ -142,8 +142,8 @@ static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
return run_guest(lg, (unsigned long __user *)user); return run_guest(lg, (unsigned long __user *)user);
} }
/*L:020 The initialization write supplies 5 32-bit values (in addition to the /*L:020 The initialization write supplies 5 pointer sized (32 or 64 bit)
* 32-bit LHREQ_INITIALIZE value). These are: * values (in addition to the LHREQ_INITIALIZE value). These are:
* *
* base: The start of the Guest-physical memory inside the Launcher memory. * base: The start of the Guest-physical memory inside the Launcher memory.
* *
@ -162,13 +162,13 @@ static ssize_t read(struct file *file, char __user *user, size_t size,loff_t*o)
* quickly converted from physical to virtual by adding PAGE_OFFSET. It's * quickly converted from physical to virtual by adding PAGE_OFFSET. It's
* 0xC0000000 (3G) by default, but it's configurable at kernel build time. * 0xC0000000 (3G) by default, but it's configurable at kernel build time.
*/ */
static int initialize(struct file *file, const u32 __user *input) static int initialize(struct file *file, const unsigned long __user *input)
{ {
/* "struct lguest" contains everything we (the Host) know about a /* "struct lguest" contains everything we (the Host) know about a
* Guest. */ * Guest. */
struct lguest *lg; struct lguest *lg;
int err; int err;
u32 args[5]; unsigned long args[5];
/* We grab the Big Lguest lock, which protects against multiple /* We grab the Big Lguest lock, which protects against multiple
* simultaneous initializations. */ * simultaneous initializations. */
@ -259,17 +259,18 @@ unlock:
* start with a 32 bit number: for the first write this must be * start with a 32 bit number: for the first write this must be
* LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use * LHREQ_INITIALIZE to set up the Guest. After that the Launcher can use
* writes of other values to get DMA buffers and send interrupts. */ * writes of other values to get DMA buffers and send interrupts. */
static ssize_t write(struct file *file, const char __user *input, static ssize_t write(struct file *file, const char __user *in,
size_t size, loff_t *off) size_t size, loff_t *off)
{ {
/* Once the guest is initialized, we hold the "struct lguest" in the /* Once the guest is initialized, we hold the "struct lguest" in the
* file private data. */ * file private data. */
struct lguest *lg = file->private_data; struct lguest *lg = file->private_data;
u32 req; const unsigned long __user *input = (const unsigned long __user *)in;
unsigned long req;
if (get_user(req, input) != 0) if (get_user(req, input) != 0)
return -EFAULT; return -EFAULT;
input += sizeof(req); input++;
/* If you haven't initialized, you must do that first. */ /* If you haven't initialized, you must do that first. */
if (req != LHREQ_INITIALIZE && !lg) if (req != LHREQ_INITIALIZE && !lg)
@ -285,13 +286,13 @@ static ssize_t write(struct file *file, const char __user *input,
switch (req) { switch (req) {
case LHREQ_INITIALIZE: case LHREQ_INITIALIZE:
return initialize(file, (const u32 __user *)input); return initialize(file, input);
case LHREQ_GETDMA: case LHREQ_GETDMA:
return user_get_dma(lg, (const u32 __user *)input); return user_get_dma(lg, input);
case LHREQ_IRQ: case LHREQ_IRQ:
return user_send_irq(lg, (const u32 __user *)input); return user_send_irq(lg, input);
case LHREQ_BREAK: case LHREQ_BREAK:
return break_guest_out(lg, (const u32 __user *)input); return break_guest_out(lg, input);
default: default:
return -EINVAL; return -EINVAL;
} }