aboutsummaryrefslogtreecommitdiffstats
path: root/xen/common/kernel.c
blob: d403717e556a1555125c326456a6d6ccc2de71de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#include <stdarg.h>
#include <xeno/lib.h>
#include <xeno/errno.h>
#include <xeno/spinlock.h>
#include <xeno/multiboot.h>
#include <xeno/sched.h>
#include <xeno/mm.h>
#include <xeno/delay.h>
#include <xeno/skbuff.h>
#include <xeno/interrupt.h>
#include <xeno/compile.h>
#include <xeno/version.h>
#include <asm/io.h>
#include <asm/msr.h>
#include <asm/uaccess.h>
#include <hypervisor-ifs/dom0_ops.h>
#include <asm/byteorder.h>
#include <linux/if_ether.h>
#include <asm/domain_page.h>
#include <xeno/console.h>
#include <xeno/net_headers.h>

static int xpos, ypos;
static volatile unsigned char *video;

spinlock_t console_lock = SPIN_LOCK_UNLOCKED;

struct e820entry {
    unsigned long addr_lo, addr_hi;        /* start of memory segment */
    unsigned long size_lo, size_hi;        /* size of memory segment */
    unsigned long type;                    /* type of memory segment */
};

void init_vga(void);
void init_serial(void);
void start_of_day(void);

/* Command line options and variables. */
unsigned int opt_console = 1;
unsigned int opt_ser_baud = 9600;  /* default baud for COM1 */
unsigned int opt_dom0_mem = 16000; /* default kbytes for DOM0 */
unsigned int opt_ne_base = 0; /* NE2k NICs cannot be probed */
unsigned char opt_ifname[10] = "eth0";
int opt_noht=0, opt_noacpi=0, opt_nosmp=0;
enum { OPT_IP, OPT_STR, OPT_UINT, OPT_BOOL };
static struct {
    unsigned char *name;
    int type;
    void *var;
} opts[] = {
    { "console",     OPT_UINT, &opt_console },
    { "ser_baud",    OPT_UINT, &opt_ser_baud },
    { "dom0_mem",    OPT_UINT, &opt_dom0_mem }, 
    { "ne_base",     OPT_UINT, &opt_ne_base },
    { "ifname",      OPT_STR,  &opt_ifname },
    { "noht",        OPT_BOOL, &opt_noht },
    { "noacpi",      OPT_BOOL, &opt_noacpi },
    { "nosmp",       OPT_BOOL, &opt_nosmp },
    { NULL,       0,        NULL     }
};


void cmain (unsigned long magic, multiboot_info_t *mbi)
{
    struct task_struct *new_dom;
    dom0_newdomain_t dom0_params;
    unsigned long max_page;
    unsigned char *cmdline;
    module_t *mod;
    int i;

    /*
     * Note that serial output cannot be done properly until 
     * after command-line arguments have been parsed, and the required baud 
     * rate is known. Any messages before that will be output using the
     * settings of the bootloader, for example. Maybe okay for error msgs...
     */
#define early_error(args...) opt_console=1; init_vga(); cls(); printk(args)

    if ( magic != MULTIBOOT_BOOTLOADER_MAGIC )
    {
        early_error("Invalid magic number: 0x%x\n", (unsigned)magic);
        return;
    }

    /*
     * We require some kind of memory and module information.
     * The rest we can fake!
     */
    if ( (mbi->flags & 9) != 9 )
    {
        early_error("Bad flags passed by bootloader: 0x%x\n", (unsigned)mbi->flags);
        return;
    }

    if ( mbi->mods_count == 0 )
    {
        early_error("Require at least one module!\n");
        return;
    }

    /* Are mmap_* valid?  */
#if 0
    if ( (mbi->flags & (1<<6)) )
    {
        memory_map_t *mmap = (memory_map_t *)mbi->mmap_addr;
        struct e820entry *e820 = E820_MAP;

        while ( (unsigned long)mmap < (mbi->mmap_addr + mbi->mmap_length) )
        {
            e820->addr_lo = mmap->base_addr_low;
            e820->addr_hi = mmap->base_addr_high;
            e820->size_lo = mmap->length_low;
            e820->size_hi = mmap->length_high;
            e820->type    = mmap->type;
            e820++;
            mmap = (memory_map_t *) 
                ((unsigned long)mmap + mmap->size + sizeof (mmap->size));
        }
    }
#endif

    mod = (module_t *)__va(mbi->mods_addr);

    /* Parse the command line. */
    cmdline = (unsigned char *)(mbi->cmdline ? __va(mbi->cmdline) : NULL);
    if ( cmdline != NULL )
    {
        unsigned char *opt_end, *opt;
        while ( *cmdline == ' ' ) cmdline++;
        cmdline = strchr(cmdline, ' ');
        while ( cmdline != NULL )
        {
            while ( *cmdline == ' ' ) cmdline++;
            if ( *cmdline == '\0' ) break;
            opt_end = strchr(cmdline, ' ');
            if ( opt_end != NULL ) *opt_end++ = '\0';
            opt = strchr(cmdline, '=');
            if ( opt != NULL ) *opt++ = '\0';
            for ( i = 0; opts[i].name != NULL; i++ )
            {
                if ( strcmp(opts[i].name, cmdline ) != 0 ) continue;
                switch ( opts[i].type )
                {
                case OPT_IP:
                    if ( opt != NULL )
                        *(unsigned long *)opts[i].var = str_to_quad(opt);
                    break;
                case OPT_STR:
                    if ( opt != NULL )
                        strcpy(opts[i].var, opt);
                    break;
                case OPT_UINT:
                    if ( opt != NULL )
                        *(unsigned int *)opts[i].var =
                            simple_strtol(opt, (char **)&opt, 0);
                    break;
                case OPT_BOOL:
                    *(int *)opts[i].var = 1;
                    break;
                }
            }
            cmdline = opt_end;
        }
    }

#undef early_error

    init_vga();
    cls();

    /* INITIALISE SERIAL LINE (printk will work okay from here on). */
    init_serial();

    printk(XEN_BANNER);
    printk(" http://www.cl.cam.ac.uk/xeno\n");
    printk(" University of Cambridge Computer Laboratory\n\n");
    printk(" Xen version %d.%d%s (%s@%s) (%s) %s\n\n",
           XEN_VERSION, XEN_SUBVERSION, XEN_EXTRAVERSION,
           XEN_COMPILE_BY, XEN_COMPILE_DOMAIN,
           XEN_COMPILER, XEN_COMPILE_DATE);

    memcpy(&idle0_task_union, &first_task_struct, sizeof(first_task_struct));

    max_page = (mbi->mem_upper+1024) >> (PAGE_SHIFT - 10);
    init_frametable(max_page);
    printk("Initialised all memory on a %luMB machine\n",
           max_page >> (20-PAGE_SHIFT));

    init_page_allocator(__pa(&_end), MAX_MONITOR_ADDRESS);
 
    /* These things will get done by do_newdomain() for all other tasks. */
    current->shared_info = (void *)get_free_page(GFP_KERNEL);
    memset(current->shared_info, 0, sizeof(shared_info_t));
    set_fs(USER_DS);

    start_of_day();

    /* Create initial domain 0. */
    dom0_params.num_vifs  = 1;
    dom0_params.memory_kb = opt_dom0_mem;

    new_dom = do_newdomain(0, 0);
    if ( new_dom == NULL ) panic("Error creating domain 0\n");

    /*
     * We're going to setup domain0 using the module(s) that we stashed safely 
     * above our MAX_DIRECTMAP_ADDRESS in boot/Boot.S The second module, if 
     * present, is an initrd ramdisk
     */
    if ( setup_guestos(new_dom, 
                       &dom0_params, 
                       (char *)MAX_DIRECTMAP_ADDRESS, 
                       mod[mbi->mods_count-1].mod_end - mod[0].mod_start,
                       __va(mod[0].string),
		       (mbi->mods_count == 2) ?
                       (mod[1].mod_end - mod[1].mod_start):0)
         != 0 ) panic("Could not set up DOM0 guest OS\n");

    update_dom_time(new_dom->shared_info);
    wake_up(new_dom);

    cpu_idle();
}    


#define SERIAL_BASE 0x3f8
#define RX_BUF      0
#define TX_HOLD     0
#define INT_ENABLE  1
#define INT_IDENT   2
#define DATA_FORMAT 3
#define LINE_CTL    4
#define LINE_STATUS 5
#define LINE_IN     6
#define DIVISOR_LO  0
#define DIVISOR_HI  1

void init_serial(void)
{
#ifdef CONFIG_OUTPUT_SERIAL
    /* 'opt_ser_baud' baud, no parity, 1 stop bit, 8 data bits. */
    outb(0x83, SERIAL_BASE+DATA_FORMAT);
    outb(115200/opt_ser_baud, SERIAL_BASE+DIVISOR_LO);
    outb(0, SERIAL_BASE+DIVISOR_HI);
    outb(0x03, SERIAL_BASE+DATA_FORMAT);

    /* No interrupts. */
    outb(0x00, SERIAL_BASE+INT_ENABLE);
#endif
}


void putchar_serial(unsigned char c)
{
#ifdef CONFIG_OUTPUT_SERIAL
    if ( c == '\n' ) putchar_serial('\r');
    while ( !(inb(SERIAL_BASE+LINE_STATUS)&(1<<5)) ) barrier();
    outb(c, SERIAL_BASE+TX_HOLD);
#endif
}


/* VGA text (mode 3) definitions. */
#define COLUMNS	    80
#define LINES	    25
#define ATTRIBUTE    7
#define VIDEO	    __va(0xB8000)

/* This is actually code from vgaHWRestore in an old version of XFree86 :-) */
void init_vga(void)
{
#ifdef CONFIG_OUTPUT_CONSOLE
    /* The following VGA state was saved from a chip in text mode 3. */
    static unsigned char regs[] = {
        /* Sequencer registers */
        0x03, 0x00, 0x03, 0x00, 0x02,
        /* CRTC registers */
        0x5f, 0x4f, 0x50, 0x82, 0x55, 0x81, 0xbf, 0x1f, 0x00, 0x4f, 0x20,
        0x0e, 0x00, 0x00, 0x01, 0xe0, 0x9c, 0x8e, 0x8f, 0x28, 0x1f, 0x96,
        0xb9, 0xa3, 0xff,
        /* Graphic registers */
        0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x0e, 0x00, 0xff,
        /* Attribute registers */
        0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x14, 0x07, 0x38, 0x39, 0x3a,
        0x3b, 0x3c, 0x3d, 0x3e, 0x3f, 0x0c, 0x00, 0x0f, 0x08, 0x00
    };

    int i, j = 0;
    volatile unsigned char tmp;

    if(opt_console) {
      tmp = inb(0x3da);
      outb(0x00, 0x3c0);
      
      for ( i = 0; i < 5;  i++ )
        outw((regs[j++] << 8) | i, 0x3c4);
      
      /* Ensure CRTC registers 0-7 are unlocked by clearing bit 7 of CRTC[17]. */
      outw(((regs[5+17] & 0x7F) << 8) | 17, 0x3d4);
      
      for ( i = 0; i < 25; i++ ) 
        outw((regs[j++] << 8) | i, 0x3d4);
      
      for ( i = 0; i < 9;  i++ )
        outw((regs[j++] << 8) | i, 0x3ce);
      
      for ( i = 0; i < 21; i++ )
	{
	  tmp = inb(0x3da);
	  outb(i, 0x3c0); 
	  outb(regs[j++], 0x3c0);
	}
      
      tmp = inb(0x3da);
      outb(0x20, 0x3c0);
    }
#endif
}


/* Clear the screen and initialize VIDEO, XPOS and YPOS.  */
void cls(void)
{
#ifdef CONFIG_OUTPUT_CONSOLE
    int i;

    if(opt_console) {
      video = (unsigned char *) VIDEO;
      
      for (i = 0; i < COLUMNS * LINES * 2; i++)
        *(video + i) = 0;
      
      xpos = 0;
      ypos = 0;
      
      outw(10+(1<<(5+8)), 0x3d4); /* cursor off */
    }
#endif
}


/* Put the character C on the screen.  */
static void putchar (int c)
{
    if ( (c != '\n') && ((c < 32) || (c > 126)) ) return;
    putchar_serial(c);
  
#ifdef CONFIG_OUTPUT_CONSOLE
    if(opt_console) {
      if (c == '\n')
	{
	newline:
	  xpos = 0;
	  ypos++;
	  if (ypos >= LINES)
	    {
	      static char zeroarr[2*COLUMNS] = { 0 };
	      ypos = LINES-1;
	      memcpy((char*)video, 
		     (char*)video + 2*COLUMNS, (LINES-1)*2*COLUMNS);
	      memcpy((char*)video + (LINES-1)*2*COLUMNS, 
		     zeroarr, 2*COLUMNS);
	    }
	  return;
	}
      
      *(video + (xpos + ypos * COLUMNS) * 2) = c & 0xFF;
      *(video + (xpos + ypos * COLUMNS) * 2 + 1) = ATTRIBUTE;
      
      xpos++;
      if (xpos >= COLUMNS)
        goto newline;
    }
#endif
}

static inline void __putstr(const char *str)
{
    while ( *str ) putchar(*str++);
}

void printf (const char *fmt, ...)
{
    va_list args;
    char buf[128];
    const char *p = fmt;
    unsigned long flags;

    /*
     * If the format string contains '%' descriptors then we have to parse it 
     * before printing it. We parse it into a fixed-length buffer. Long 
     * strings should therefore _not_ contain '%' characters!
     */
    if ( strchr(fmt, '%') != NULL )
    {
        va_start(args, fmt);
        (void)vsnprintf(buf, sizeof(buf), fmt, args);
        va_end(args);        
        p = buf; 
    }

    spin_lock_irqsave(&console_lock, flags);
    while ( *p ) putchar(*p++);
    spin_unlock_irqrestore(&console_lock, flags);
}

void panic(const char *fmt, ...)
{
    va_list args;
    char buf[128];
    unsigned long flags;
    extern void machine_restart(char *);
    
    va_start(args, fmt);
    (void)vsnprintf(buf, sizeof(buf), fmt, args);
    va_end(args);
    
    /* Spit out multiline message in one go. */
    spin_lock_irqsave(&console_lock, flags);
    __putstr("\n****************************************\n");
    __putstr(buf);
    __putstr("Aieee! CPU");
    sprintf(buf, "%d", smp_processor_id());
    __putstr(buf);
    __putstr(" is toast...\n");
    __putstr("****************************************\n\n");
    __putstr("Reboot in five seconds...\n");
    spin_unlock_irqrestore(&console_lock, flags);

    mdelay(5000);
    machine_restart(0);
}

/* No-op syscall. */
asmlinkage long sys_ni_syscall(void)
{
    return -ENOSYS;
}


unsigned short compute_cksum(unsigned short *buf, int count)
{
	/* Function written by ek247
	 * Computes IP and UDP checksum.
	 * To be used for the fake console packets
	 * created in console_export
	 */

    unsigned long sum=0;

    while (count--)
    {
        sum+=*buf++;
        if (sum & 0xFFFF0000)
        {
            //carry occured, so wrap around
            sum &=0xFFFF;
            sum++;
        }
    }
    return ~(sum & 0xFFFF);
}



/* XXX SMH: below is rather vile; pulled in to allow network console */

extern int netif_rx(struct sk_buff *); 

/*
 * Function written by ek247. Exports console output from all domains upwards 
 * to domain0, by stuffing it into a fake network packet.
 */
int console_export(char *str, int len)
{
    struct sk_buff *skb;
    struct iphdr *iph = NULL;  
    struct udphdr *udph = NULL; 
    struct ethhdr *ethh = NULL; 
    int hdr_size = sizeof(struct iphdr) + sizeof(struct udphdr); 
    u8 *skb_data;

    skb = dev_alloc_skb(sizeof(struct ethhdr) + 
                                   hdr_size + len + 20);
    if ( skb == NULL ) return 0;

    skb->dev = the_dev;
    skb_data = (u8 *)map_domain_mem((skb->pf - frame_table) << PAGE_SHIFT);
    skb_reserve(skb, 2);

    /* Get a pointer to each header. */
    ethh = (struct ethhdr *) 
        (skb_data + (skb->data - skb->head));
    iph  = (struct iphdr *)(ethh + 1);
    udph = (struct udphdr *)(iph + 1); 

    skb_reserve(skb, sizeof(struct ethhdr)); 
    skb_put(skb, hdr_size + len); 

    /* Build IP header. */
    iph->version = 4;
    iph->ihl     = 5;
    iph->frag_off= 0;
    iph->id      = 0xdead;
    iph->ttl     = 255;
    iph->protocol= 17;
    iph->daddr   = htonl(0xa9fe0100);  /* 169.254.1.0 */
    iph->saddr   = htonl(0xa9fefeff);  /* 169.254.254.255 */
    iph->tot_len = htons(hdr_size + len); 
    iph->check	 = 0;
    iph->check   = compute_cksum((__u16 *)iph, sizeof(struct iphdr)/2); 

    /* Build UDP header. */
    udph->source = htons(current->domain);
    udph->dest   = htons(666);
    udph->len    = htons(sizeof(struct udphdr) + len);
    udph->check  = 0;

    /* Build the UDP payload. */
    memcpy((char *)(udph + 1), str, len); 

    /* Fix Ethernet header. */
    memset(ethh->h_source, 0, ETH_ALEN);
    memset(ethh->h_dest,   0, ETH_ALEN);
    ethh->h_proto = htons(ETH_P_IP);
    skb->mac.ethernet= (struct ethhdr *)ethh;

    unmap_domain_mem(skb_data);
    
    skb->dst_vif = find_vif_by_id(0);
    (void)netif_rx(skb);

    return 1;
}


long do_console_write(char *str, unsigned int count)
{
#define SIZEOF_BUF 256
    unsigned char safe_str[SIZEOF_BUF];
    unsigned char exported_str[SIZEOF_BUF+1];
    unsigned char dom_id[5];
    unsigned long flags;
    int i=0;
    int j=0;
    unsigned char prev = '\n';
    
    if ( count > SIZEOF_BUF ) count = SIZEOF_BUF;
    
    if ( copy_from_user(safe_str, str, count) )
        return -EFAULT;
    
    spin_lock_irqsave(&console_lock, flags);

    __putstr("DOM"); 
    sprintf(dom_id, "%d", current->domain);
    __putstr(dom_id);
    __putstr(": ");
    
    for ( i = 0; i < count; i++ )
    {
	exported_str[j++]=safe_str[i];
	
        if ( !safe_str[i] ) break;
        putchar(prev = safe_str[i]);
    }
    
    if ( prev != '\n' ) putchar('\n');
    
    spin_unlock_irqrestore(&console_lock, flags);
    
    exported_str[j++]='\0';
    console_export(exported_str, j);
    
    return(0);
}

void __out_of_line_bug(int line)
{
    printk("kernel BUG in header file at line %d\n", line);
    BUG();
    for ( ; ; ) continue;
}