Merge branch 'vendor/LIBARCHIVE'
[dragonfly.git] / sys / platform / vkernel64 / platform / init.c
1 /*
2  * Copyright (c) 2006 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34
35 #include <sys/types.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/stat.h>
39 #include <sys/mman.h>
40 #include <sys/cons.h>
41 #include <sys/random.h>
42 #include <sys/vkernel.h>
43 #include <sys/tls.h>
44 #include <sys/reboot.h>
45 #include <sys/proc.h>
46 #include <sys/msgbuf.h>
47 #include <sys/vmspace.h>
48 #include <sys/socket.h>
49 #include <sys/sockio.h>
50 #include <sys/sysctl.h>
51 #include <sys/un.h>
52 #include <vm/vm_page.h>
53 #include <vm/vm_map.h>
54 #include <sys/mplock2.h>
55
56 #include <machine/cpu.h>
57 #include <machine/globaldata.h>
58 #include <machine/tls.h>
59 #include <machine/md_var.h>
60 #include <machine/vmparam.h>
61 #include <cpu/specialreg.h>
62
63 #include <net/if.h>
64 #include <net/if_arp.h>
65 #include <net/ethernet.h>
66 #include <net/bridge/if_bridgevar.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <net/if_var.h>
70
71 #include <stdio.h>
72 #include <stdlib.h>
73 #include <stdarg.h>
74 #include <stdbool.h>
75 #include <unistd.h>
76 #include <fcntl.h>
77 #include <string.h>
78 #include <err.h>
79 #include <errno.h>
80 #include <assert.h>
81 #include <sysexits.h>
82
83 vm_paddr_t phys_avail[16];
84 vm_paddr_t Maxmem;
85 vm_paddr_t Maxmem_bytes;
86 long physmem;
87 int MemImageFd = -1;
88 struct vkdisk_info DiskInfo[VKDISK_MAX];
89 int DiskNum;
90 struct vknetif_info NetifInfo[VKNETIF_MAX];
91 int NetifNum;
92 char *pid_file;
93 vm_offset_t KvaStart;
94 vm_offset_t KvaEnd;
95 vm_offset_t KvaSize;
96 vm_offset_t virtual_start;
97 vm_offset_t virtual_end;
98 vm_offset_t virtual2_start;
99 vm_offset_t virtual2_end;
100 vm_offset_t kernel_vm_end;
101 vm_offset_t crashdumpmap;
102 vm_offset_t clean_sva;
103 vm_offset_t clean_eva;
104 struct msgbuf *msgbufp;
105 caddr_t ptvmmap;
106 vpte_t  *KernelPTD;
107 vpte_t  *KernelPTA;     /* Warning: Offset for direct VA translation */
108 void *dmap_min_address;
109 u_int cpu_feature;      /* XXX */
110 int tsc_present;
111 int64_t tsc_frequency;
112 int optcpus;            /* number of cpus - see mp_start() */
113 int lwp_cpu_lock;       /* if/how to lock virtual CPUs to real CPUs */
114 int real_ncpus;         /* number of real CPUs */
115 int next_cpu;           /* next real CPU to lock a virtual CPU to */
116 int vkernel_b_arg;      /* -b argument - no of logical CPU bits - only SMP */
117 int vkernel_B_arg;      /* -B argument - no of core bits - only SMP */
118
119 struct privatespace *CPU_prvspace;
120
121 static struct trapframe proc0_tf;
122 static void *proc0paddr;
123
124 static void init_sys_memory(char *imageFile);
125 static void init_kern_memory(void);
126 static void init_globaldata(void);
127 static void init_vkernel(void);
128 static void init_disk(char *diskExp[], int diskFileNum, enum vkdisk_type type);
129 static void init_netif(char *netifExp[], int netifFileNum);
130 static void writepid(void);
131 static void cleanpid(void);
132 static int unix_connect(const char *path);
133 static void usage_err(const char *ctl, ...);
134 static void usage_help(_Bool);
135 static void init_locks(void);
136
137 static int save_ac;
138 static char **save_av;
139
140 /*
141  * Kernel startup for virtual kernels - standard main()
142  */
143 int
144 main(int ac, char **av)
145 {
146         char *memImageFile = NULL;
147         char *netifFile[VKNETIF_MAX];
148         char *diskFile[VKDISK_MAX];
149         char *cdFile[VKDISK_MAX];
150         char *suffix;
151         char *endp;
152         char *tmp;
153         char *tok;
154         int netifFileNum = 0;
155         int diskFileNum = 0;
156         int cdFileNum = 0;
157         int bootOnDisk = -1;    /* set below to vcd (0) or vkd (1) */
158         int c;
159         int i;
160         int j;
161         int n;
162         int isq;
163         int pos;
164         int eflag;
165         int real_vkernel_enable;
166         int supports_sse;
167         size_t vsize;
168         size_t kenv_size;
169         size_t kenv_size2;
170
171         save_ac = ac;
172         save_av = av;
173         eflag = 0;
174         pos = 0;
175         kenv_size = 0;
176
177         /*
178          * Process options
179          */
180         kernel_mem_readonly = 1;
181         optcpus = 2;
182         vkernel_b_arg = 0;
183         vkernel_B_arg = 0;
184         lwp_cpu_lock = LCL_NONE;
185
186         real_vkernel_enable = 0;
187         vsize = sizeof(real_vkernel_enable);
188         sysctlbyname("vm.vkernel_enable", &real_vkernel_enable, &vsize, NULL,0);
189
190         if (real_vkernel_enable == 0) {
191                 errx(1, "vm.vkernel_enable is 0, must be set "
192                         "to 1 to execute a vkernel!");
193         }
194
195         real_ncpus = 1;
196         vsize = sizeof(real_ncpus);
197         sysctlbyname("hw.ncpu", &real_ncpus, &vsize, NULL, 0);
198
199         if (ac < 2)
200                 usage_help(false);
201
202         while ((c = getopt(ac, av, "c:hsvl:m:n:r:e:i:p:I:Ub:B:")) != -1) {
203                 switch(c) {
204                 case 'e':
205                         /*
206                          * name=value:name=value:name=value...
207                          * name="value"...
208                          *
209                          * Allow values to be quoted but note that shells
210                          * may remove the quotes, so using this feature
211                          * to embed colons may require a backslash.
212                          */
213                         n = strlen(optarg);
214                         isq = 0;
215
216                         if (eflag == 0) {
217                                 kenv_size = n + 2;
218                                 kern_envp = malloc(kenv_size);
219                                 if (kern_envp == NULL)
220                                         errx(1, "Couldn't allocate %zd bytes for kern_envp", kenv_size);
221                         } else {
222                                 kenv_size2 = kenv_size + n + 1;
223                                 pos = kenv_size - 1;
224                                 if ((tmp = realloc(kern_envp, kenv_size2)) == NULL)
225                                         errx(1, "Couldn't reallocate %zd bytes for kern_envp", kenv_size2);
226                                 kern_envp = tmp;
227                                 kenv_size = kenv_size2;
228                         }
229
230                         for (i = 0, j = pos; i < n; ++i) {
231                                 if (optarg[i] == '"')
232                                         isq ^= 1;
233                                 else if (optarg[i] == '\'')
234                                         isq ^= 2;
235                                 else if (isq == 0 && optarg[i] == ':')
236                                         kern_envp[j++] = 0;
237                                 else
238                                         kern_envp[j++] = optarg[i];
239                         }
240                         kern_envp[j++] = 0;
241                         kern_envp[j++] = 0;
242                         eflag++;
243                         break;
244                 case 's':
245                         boothowto |= RB_SINGLE;
246                         break;
247                 case 'v':
248                         bootverbose = 1;
249                         break;
250                 case 'i':
251                         memImageFile = optarg;
252                         break;
253                 case 'I':
254                         if (netifFileNum < VKNETIF_MAX)
255                                 netifFile[netifFileNum++] = strdup(optarg);
256                         break;
257                 case 'r':
258                         if (bootOnDisk < 0)
259                                 bootOnDisk = 1;
260                         if (diskFileNum + cdFileNum < VKDISK_MAX)
261                                 diskFile[diskFileNum++] = strdup(optarg);
262                         break;
263                 case 'c':
264                         if (bootOnDisk < 0)
265                                 bootOnDisk = 0;
266                         if (diskFileNum + cdFileNum < VKDISK_MAX)
267                                 cdFile[cdFileNum++] = strdup(optarg);
268                         break;
269                 case 'm':
270                         Maxmem_bytes = strtoull(optarg, &suffix, 0);
271                         if (suffix) {
272                                 switch(*suffix) {
273                                 case 'g':
274                                 case 'G':
275                                         Maxmem_bytes <<= 30;
276                                         break;
277                                 case 'm':
278                                 case 'M':
279                                         Maxmem_bytes <<= 20;
280                                         break;
281                                 case 'k':
282                                 case 'K':
283                                         Maxmem_bytes <<= 10;
284                                         break;
285                                 default:
286                                         Maxmem_bytes = 0;
287                                         usage_err("Bad maxmem option");
288                                         /* NOT REACHED */
289                                         break;
290                                 }
291                         }
292                         break;
293                 case 'l':
294                         next_cpu = -1;
295                         if (strncmp("map", optarg, 3) == 0) {
296                                 lwp_cpu_lock = LCL_PER_CPU;
297                                 if (optarg[3] == ',') {
298                                         next_cpu = strtol(optarg+4, &endp, 0);
299                                         if (*endp != '\0')
300                                                 usage_err("Bad target CPU number at '%s'", endp);
301                                 } else {
302                                         next_cpu = 0;
303                                 }
304                                 if (next_cpu < 0 || next_cpu > real_ncpus - 1)
305                                         usage_err("Bad target CPU, valid range is 0-%d", real_ncpus - 1);
306                         } else if (strncmp("any", optarg, 3) == 0) {
307                                 lwp_cpu_lock = LCL_NONE;
308                         } else {
309                                 lwp_cpu_lock = LCL_SINGLE_CPU;
310                                 next_cpu = strtol(optarg, &endp, 0);
311                                 if (*endp != '\0')
312                                         usage_err("Bad target CPU number at '%s'", endp);
313                                 if (next_cpu < 0 || next_cpu > real_ncpus - 1)
314                                         usage_err("Bad target CPU, valid range is 0-%d", real_ncpus - 1);
315                         }
316                         break;
317                 case 'n':
318                         /*
319                          * This value is set up by mp_start(), don't just
320                          * set ncpus here.
321                          */
322                         tok = strtok(optarg, ":");
323                         optcpus = strtol(tok, NULL, 0);
324                         if (optcpus < 1 || optcpus > MAXCPU)
325                                 usage_err("Bad ncpus, valid range is 1-%d", MAXCPU);
326                         
327                         /* :lbits argument */
328                         tok = strtok(NULL, ":");
329                         if (tok != NULL) {
330                                 vkernel_b_arg = strtol(tok, NULL, 0);
331
332                                 /* :cbits argument */
333                                 tok = strtok(NULL, ":");
334                                 if (tok != NULL) {
335                                         vkernel_B_arg = strtol(tok, NULL, 0);
336                                 }
337
338                         }
339                         break;
340                 case 'p':
341                         pid_file = optarg;
342                         break;
343                 case 'U':
344                         kernel_mem_readonly = 0;
345                         break;
346                 case 'h':
347                         usage_help(true);
348                         break;
349                 default:
350                         usage_help(false);
351                 }
352         }
353
354         writepid();
355         cpu_disable_intr();
356         init_sys_memory(memImageFile);
357         init_kern_memory();
358         init_globaldata();
359         init_vkernel();
360         setrealcpu();
361         init_kqueue();
362
363         vmm_guest = 1;
364
365         /*
366          * Check TSC
367          */
368         vsize = sizeof(tsc_present);
369         sysctlbyname("hw.tsc_present", &tsc_present, &vsize, NULL, 0);
370         vsize = sizeof(tsc_frequency);
371         sysctlbyname("hw.tsc_frequency", &tsc_frequency, &vsize, NULL, 0);
372         if (tsc_present)
373                 cpu_feature |= CPUID_TSC;
374
375         /*
376          * Check SSE
377          */
378         vsize = sizeof(supports_sse);
379         supports_sse = 0;
380         sysctlbyname("hw.instruction_sse", &supports_sse, &vsize, NULL, 0);
381         init_fpu(supports_sse);
382         if (supports_sse)
383                 cpu_feature |= CPUID_SSE | CPUID_FXSR;
384
385         /*
386          * We boot from the first installed disk.
387          */
388         if (bootOnDisk == 1) {
389                 init_disk(diskFile, diskFileNum, VKD_DISK);
390                 init_disk(cdFile, cdFileNum, VKD_CD);
391         } else {
392                 init_disk(cdFile, cdFileNum, VKD_CD);
393                 init_disk(diskFile, diskFileNum, VKD_DISK);
394         }
395         init_netif(netifFile, netifFileNum);
396         init_exceptions();
397         mi_startup();
398         /* NOT REACHED */
399         exit(EX_SOFTWARE);
400 }
401
402 /*
403  * Initialize system memory.  This is the virtual kernel's 'RAM'.
404  */
405 static
406 void
407 init_sys_memory(char *imageFile)
408 {
409         struct stat st;
410         int i;
411         int fd;
412
413         /*
414          * Figure out the system memory image size.  If an image file was
415          * specified and -m was not specified, use the image file's size.
416          */
417         if (imageFile && stat(imageFile, &st) == 0 && Maxmem_bytes == 0)
418                 Maxmem_bytes = (vm_paddr_t)st.st_size;
419         if ((imageFile == NULL || stat(imageFile, &st) < 0) &&
420             Maxmem_bytes == 0) {
421                 errx(1, "Cannot create new memory file %s unless "
422                        "system memory size is specified with -m",
423                        imageFile);
424                 /* NOT REACHED */
425         }
426
427         /*
428          * Maxmem must be known at this time
429          */
430         if (Maxmem_bytes < 64 * 1024 * 1024 || (Maxmem_bytes & SEG_MASK)) {
431                 errx(1, "Bad maxmem specification: 64MB minimum, "
432                        "multiples of %dMB only",
433                        SEG_SIZE / 1024 / 1024);
434                 /* NOT REACHED */
435         }
436
437         /*
438          * Generate an image file name if necessary, then open/create the
439          * file exclusively locked.  Do not allow multiple virtual kernels
440          * to use the same image file.
441          *
442          * Don't iterate through a million files if we do not have write
443          * access to the directory, stop if our open() failed on a
444          * non-existant file.  Otherwise opens can fail for any number
445          */
446         if (imageFile == NULL) {
447                 for (i = 0; i < 1000000; ++i) {
448                         asprintf(&imageFile, "/var/vkernel/memimg.%06d", i);
449                         fd = open(imageFile,
450                                   O_RDWR|O_CREAT|O_EXLOCK|O_NONBLOCK, 0644);
451                         if (fd < 0 && stat(imageFile, &st) == 0) {
452                                 free(imageFile);
453                                 continue;
454                         }
455                         break;
456                 }
457         } else {
458                 fd = open(imageFile, O_RDWR|O_CREAT|O_EXLOCK|O_NONBLOCK, 0644);
459         }
460         fprintf(stderr, "Using memory file: %s\n", imageFile);
461         if (fd < 0 || fstat(fd, &st) < 0) {
462                 err(1, "Unable to open/create %s", imageFile);
463                 /* NOT REACHED */
464         }
465
466         /*
467          * Truncate or extend the file as necessary.  Clean out the contents
468          * of the file, we want it to be full of holes so we don't waste
469          * time reading in data from an old file that we no longer care
470          * about.
471          */
472         ftruncate(fd, 0);
473         ftruncate(fd, Maxmem_bytes);
474
475         MemImageFd = fd;
476         Maxmem = Maxmem_bytes >> PAGE_SHIFT;
477         physmem = Maxmem;
478 }
479
480 /*
481  * Initialize kernel memory.  This reserves kernel virtual memory by using
482  * MAP_VPAGETABLE
483  */
484
485 static
486 void
487 init_kern_memory(void)
488 {
489         void *base;
490         void *try;
491         char dummy;
492         char *topofstack = &dummy;
493         int i;
494         void *firstfree;
495
496         /*
497          * Memory map our kernel virtual memory space.  Note that the
498          * kernel image itself is not made part of this memory for the
499          * moment.
500          *
501          * The memory map must be segment-aligned so we can properly
502          * offset KernelPTD.
503          *
504          * If the system kernel has a different MAXDSIZ, it might not
505          * be possible to map kernel memory in its prefered location.
506          * Try a number of different locations.
507          */
508         try = (void *)(512UL << 30);
509         base = NULL;
510         while ((char *)try + KERNEL_KVA_SIZE < topofstack) {
511                 base = mmap(try, KERNEL_KVA_SIZE, PROT_READ|PROT_WRITE,
512                             MAP_FILE|MAP_SHARED|MAP_VPAGETABLE,
513                             MemImageFd, (off_t)try);
514                 if (base == try)
515                         break;
516                 if (base != MAP_FAILED)
517                         munmap(base, KERNEL_KVA_SIZE);
518                 try = (char *)try + (512UL << 30);
519         }
520         if (base != try) {
521                 err(1, "Unable to mmap() kernel virtual memory!");
522                 /* NOT REACHED */
523         }
524         madvise(base, KERNEL_KVA_SIZE, MADV_NOSYNC);
525         KvaStart = (vm_offset_t)base;
526         KvaSize = KERNEL_KVA_SIZE;
527         KvaEnd = KvaStart + KvaSize;
528
529         /* cannot use kprintf yet */
530         printf("KVM mapped at %p-%p\n", (void *)KvaStart, (void *)KvaEnd);
531
532         /* MAP_FILE? */
533         dmap_min_address = mmap(0, DMAP_SIZE, PROT_READ|PROT_WRITE,
534                                 MAP_NOCORE|MAP_NOSYNC|MAP_SHARED,
535                                 MemImageFd, 0);
536         if (dmap_min_address == MAP_FAILED) {
537                 err(1, "Unable to mmap() kernel DMAP region!");
538                 /* NOT REACHED */
539         }
540
541         firstfree = NULL;
542         pmap_bootstrap((vm_paddr_t *)&firstfree, (int64_t)base);
543
544         mcontrol(base, KERNEL_KVA_SIZE, MADV_SETMAP,
545                  0 | VPTE_R | VPTE_W | VPTE_V);
546
547         /*
548          * phys_avail[] represents unallocated physical memory.  MI code
549          * will use phys_avail[] to create the vm_page array.
550          */
551         phys_avail[0] = (vm_paddr_t)firstfree;
552         phys_avail[0] = (phys_avail[0] + PAGE_MASK) & ~(vm_paddr_t)PAGE_MASK;
553         phys_avail[1] = Maxmem_bytes;
554
555 #if JGV
556         /*
557          * (virtual_start, virtual_end) represent unallocated kernel virtual
558          * memory.  MI code will create kernel_map using these parameters.
559          */
560         virtual_start = KvaStart + (long)firstfree;
561         virtual_start = (virtual_start + PAGE_MASK) & ~(vm_offset_t)PAGE_MASK;
562         virtual_end = KvaStart + KERNEL_KVA_SIZE;
563 #endif
564
565         /*
566          * pmap_growkernel() will set the correct value.
567          */
568         kernel_vm_end = 0;
569
570         /*
571          * Allocate space for process 0's UAREA.
572          */
573         proc0paddr = (void *)virtual_start;
574         for (i = 0; i < UPAGES; ++i) {
575                 pmap_kenter_quick(virtual_start, phys_avail[0]);
576                 virtual_start += PAGE_SIZE;
577                 phys_avail[0] += PAGE_SIZE;
578         }
579
580         /*
581          * crashdumpmap
582          */
583         crashdumpmap = virtual_start;
584         virtual_start += MAXDUMPPGS * PAGE_SIZE;
585
586         /*
587          * msgbufp maps the system message buffer
588          */
589         assert((MSGBUF_SIZE & PAGE_MASK) == 0);
590         msgbufp = (void *)virtual_start;
591         for (i = 0; i < (MSGBUF_SIZE >> PAGE_SHIFT); ++i) {
592                 pmap_kenter_quick(virtual_start, phys_avail[0]);
593                 virtual_start += PAGE_SIZE;
594                 phys_avail[0] += PAGE_SIZE;
595         }
596         msgbufinit(msgbufp, MSGBUF_SIZE);
597
598         /*
599          * used by kern_memio for /dev/mem access
600          */
601         ptvmmap = (caddr_t)virtual_start;
602         virtual_start += PAGE_SIZE;
603
604         /*
605          * Bootstrap the kernel_pmap
606          */
607 #if JGV
608         pmap_bootstrap();
609 #endif
610 }
611
612 /*
613  * Map the per-cpu globaldata for cpu #0.  Allocate the space using
614  * virtual_start and phys_avail[0]
615  */
616 static
617 void
618 init_globaldata(void)
619 {
620         int i;
621         vm_paddr_t pa;
622         vm_offset_t va;
623
624         /*
625          * Reserve enough KVA to cover possible cpus.  This is a considerable
626          * amount of KVA since the privatespace structure includes two
627          * whole page table mappings.
628          */
629         virtual_start = (virtual_start + SEG_MASK) & ~(vm_offset_t)SEG_MASK;
630         CPU_prvspace = (void *)virtual_start;
631         virtual_start += sizeof(struct privatespace) * SMP_MAXCPU;
632
633         /*
634          * Allocate enough physical memory to cover the mdglobaldata
635          * portion of the space and the idle stack and map the pages
636          * into KVA.  For cpu #0 only.
637          */
638         for (i = 0; i < sizeof(struct mdglobaldata); i += PAGE_SIZE) {
639                 pa = phys_avail[0];
640                 va = (vm_offset_t)&CPU_prvspace[0].mdglobaldata + i;
641                 pmap_kenter_quick(va, pa);
642                 phys_avail[0] += PAGE_SIZE;
643         }
644         for (i = 0; i < sizeof(CPU_prvspace[0].idlestack); i += PAGE_SIZE) {
645                 pa = phys_avail[0];
646                 va = (vm_offset_t)&CPU_prvspace[0].idlestack + i;
647                 pmap_kenter_quick(va, pa);
648                 phys_avail[0] += PAGE_SIZE;
649         }
650
651         /*
652          * Setup the %gs for cpu #0.  The mycpu macro works after this
653          * point.  Note that %fs is used by pthreads.
654          */
655         tls_set_gs(&CPU_prvspace[0], sizeof(struct privatespace));
656 }
657
658
659 /*
660  * Initialize pool tokens and other necessary locks
661  */
662 static void
663 init_locks(void)
664 {
665
666         /*
667          * Get the initial mplock with a count of 1 for the BSP.
668          * This uses a LOGICAL cpu ID, ie BSP == 0.
669          */
670         cpu_get_initial_mplock();
671
672         /* our token pool needs to work early */
673         lwkt_token_pool_init();
674
675 }
676
677
678 /*
679  * Initialize very low level systems including thread0, proc0, etc.
680  */
681 static
682 void
683 init_vkernel(void)
684 {
685         struct mdglobaldata *gd;
686
687         gd = &CPU_prvspace[0].mdglobaldata;
688         bzero(gd, sizeof(*gd));
689
690         gd->mi.gd_curthread = &thread0;
691         thread0.td_gd = &gd->mi;
692         ncpus = 1;
693         ncpus2 = 1;     /* rounded down power of 2 */
694         ncpus_fit = 1;  /* rounded up power of 2 */
695         /* ncpus2_mask and ncpus_fit_mask are 0 */
696         init_param1();
697         gd->mi.gd_prvspace = &CPU_prvspace[0];
698         mi_gdinit(&gd->mi, 0);
699         cpu_gdinit(gd, 0);
700         mi_proc0init(&gd->mi, proc0paddr);
701         lwp0.lwp_md.md_regs = &proc0_tf;
702
703         init_locks();
704         cninit();
705         rand_initialize();
706 #if 0   /* #ifdef DDB */
707         kdb_init();
708         if (boothowto & RB_KDB)
709                 Debugger("Boot flags requested debugger");
710 #endif
711         identcpu();
712 #if 0
713         initializecpu();        /* Initialize CPU registers */
714 #endif
715         init_param2((phys_avail[1] - phys_avail[0]) / PAGE_SIZE);
716
717 #if 0
718         /*
719          * Map the message buffer
720          */
721         for (off = 0; off < round_page(MSGBUF_SIZE); off += PAGE_SIZE)
722                 pmap_kenter((vm_offset_t)msgbufp + off, avail_end + off);
723         msgbufinit(msgbufp, MSGBUF_SIZE);
724 #endif
725 #if 0
726         thread0.td_pcb_cr3 ... MMU
727         lwp0.lwp_md.md_regs = &proc0_tf;
728 #endif
729 }
730
731 /*
732  * Filesystem image paths for the virtual kernel are optional.
733  * If specified they each should point to a disk image,
734  * the first of which will become the root disk.
735  *
736  * The virtual kernel caches data from our 'disk' just like a normal kernel,
737  * so we do not really want the real kernel to cache the data too.  Use
738  * O_DIRECT to remove the duplication.
739  */
740 static
741 void
742 init_disk(char *diskExp[], int diskFileNum, enum vkdisk_type type)
743 {
744         int i;
745
746         if (diskFileNum == 0)
747                 return;
748
749         for(i=0; i < diskFileNum; i++){
750                 char *fname;
751                 fname = diskExp[i];
752
753                 if (fname == NULL) {
754                         warnx("Invalid argument to '-r'");
755                         continue;
756                 }
757
758                 if (DiskNum < VKDISK_MAX) {
759                         struct stat st;
760                         struct vkdisk_info* info = NULL;
761                         int fd;
762                         size_t l = 0;
763
764                         if (type == VKD_DISK)
765                             fd = open(fname, O_RDWR|O_DIRECT, 0644);
766                         else
767                             fd = open(fname, O_RDONLY|O_DIRECT, 0644);
768                         if (fd < 0 || fstat(fd, &st) < 0) {
769                                 err(1, "Unable to open/create %s", fname);
770                                 /* NOT REACHED */
771                         }
772                         if (S_ISREG(st.st_mode)) {
773                                 if (flock(fd, LOCK_EX|LOCK_NB) < 0) {
774                                         errx(1, "Disk image %s is already "
775                                                 "in use\n", fname);
776                                         /* NOT REACHED */
777                                 }
778                         }
779
780                         info = &DiskInfo[DiskNum];
781                         l = strlen(fname);
782
783                         info->unit = i;
784                         info->fd = fd;
785                         info->type = type;
786                         memcpy(info->fname, fname, l);
787
788                         if (DiskNum == 0) {
789                                 if (type == VKD_CD) {
790                                     rootdevnames[0] = "cd9660:vcd0a";
791                                 } else if (type == VKD_DISK) {
792                                     rootdevnames[0] = "ufs:vkd0s0a";
793                                     rootdevnames[1] = "ufs:vkd0s1a";
794                                 }
795                         }
796
797                         DiskNum++;
798                 } else {
799                         warnx("vkd%d (%s) > VKDISK_MAX", DiskNum, fname);
800                         continue;
801                 }
802         }
803 }
804
805 static
806 int
807 netif_set_tapflags(int tap_unit, int f, int s)
808 {
809         struct ifreq ifr;
810         int flags;
811
812         bzero(&ifr, sizeof(ifr));
813
814         snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tap%d", tap_unit);
815         if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) {
816                 warn("tap%d: ioctl(SIOCGIFFLAGS) failed", tap_unit);
817                 return -1;
818         }
819
820         /*
821          * Adjust if_flags
822          *
823          * If the flags are already set/cleared, then we return
824          * immediately to avoid extra syscalls
825          */
826         flags = (ifr.ifr_flags & 0xffff) | (ifr.ifr_flagshigh << 16);
827         if (f < 0) {
828                 /* Turn off flags */
829                 f = -f;
830                 if ((flags & f) == 0)
831                         return 0;
832                 flags &= ~f;
833         } else {
834                 /* Turn on flags */
835                 if (flags & f)
836                         return 0;
837                 flags |= f;
838         }
839
840         /*
841          * Fix up ifreq.ifr_name, since it may be trashed
842          * in previous ioctl(SIOCGIFFLAGS)
843          */
844         snprintf(ifr.ifr_name, sizeof(ifr.ifr_name), "tap%d", tap_unit);
845
846         ifr.ifr_flags = flags & 0xffff;
847         ifr.ifr_flagshigh = flags >> 16;
848         if (ioctl(s, SIOCSIFFLAGS, &ifr) < 0) {
849                 warn("tap%d: ioctl(SIOCSIFFLAGS) failed", tap_unit);
850                 return -1;
851         }
852         return 0;
853 }
854
855 static
856 int
857 netif_set_tapaddr(int tap_unit, in_addr_t addr, in_addr_t mask, int s)
858 {
859         struct ifaliasreq ifra;
860         struct sockaddr_in *in;
861
862         bzero(&ifra, sizeof(ifra));
863         snprintf(ifra.ifra_name, sizeof(ifra.ifra_name), "tap%d", tap_unit);
864
865         /* Setup address */
866         in = (struct sockaddr_in *)&ifra.ifra_addr;
867         in->sin_family = AF_INET;
868         in->sin_len = sizeof(*in);
869         in->sin_addr.s_addr = addr;
870
871         if (mask != 0) {
872                 /* Setup netmask */
873                 in = (struct sockaddr_in *)&ifra.ifra_mask;
874                 in->sin_len = sizeof(*in);
875                 in->sin_addr.s_addr = mask;
876         }
877
878         if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
879                 warn("tap%d: ioctl(SIOCAIFADDR) failed", tap_unit);
880                 return -1;
881         }
882         return 0;
883 }
884
885 static
886 int
887 netif_add_tap2brg(int tap_unit, const char *ifbridge, int s)
888 {
889         struct ifbreq ifbr;
890         struct ifdrv ifd;
891
892         bzero(&ifbr, sizeof(ifbr));
893         snprintf(ifbr.ifbr_ifsname, sizeof(ifbr.ifbr_ifsname),
894                  "tap%d", tap_unit);
895
896         bzero(&ifd, sizeof(ifd));
897         strlcpy(ifd.ifd_name, ifbridge, sizeof(ifd.ifd_name));
898         ifd.ifd_cmd = BRDGADD;
899         ifd.ifd_len = sizeof(ifbr);
900         ifd.ifd_data = &ifbr;
901
902         if (ioctl(s, SIOCSDRVSPEC, &ifd) < 0) {
903                 /*
904                  * 'errno == EEXIST' means that the tap(4) is already
905                  * a member of the bridge(4)
906                  */
907                 if (errno != EEXIST) {
908                         warn("ioctl(%s, SIOCSDRVSPEC) failed", ifbridge);
909                         return -1;
910                 }
911         }
912         return 0;
913 }
914
915 #define TAPDEV_OFLAGS   (O_RDWR | O_NONBLOCK)
916
917 /*
918  * Locate the first unused tap(4) device file if auto mode is requested,
919  * or open the user supplied device file, and bring up the corresponding
920  * tap(4) interface.
921  *
922  * NOTE: Only tap(4) device file is supported currently
923  */
924 static
925 int
926 netif_open_tap(const char *netif, int *tap_unit, int s)
927 {
928         char tap_dev[MAXPATHLEN];
929         int tap_fd, failed;
930         struct stat st;
931         char *dname;
932
933         *tap_unit = -1;
934
935         if (strcmp(netif, "auto") == 0) {
936                 /*
937                  * Find first unused tap(4) device file
938                  */
939                 tap_fd = open("/dev/tap", TAPDEV_OFLAGS);
940                 if (tap_fd < 0) {
941                         warnc(errno, "Unable to find a free tap(4)");
942                         return -1;
943                 }
944         } else {
945                 /*
946                  * User supplied tap(4) device file or unix socket.
947                  */
948                 if (netif[0] == '/')    /* Absolute path */
949                         strlcpy(tap_dev, netif, sizeof(tap_dev));
950                 else
951                         snprintf(tap_dev, sizeof(tap_dev), "/dev/%s", netif);
952
953                 tap_fd = open(tap_dev, TAPDEV_OFLAGS);
954
955                 /*
956                  * If we cannot open normally try to connect to it.
957                  */
958                 if (tap_fd < 0)
959                         tap_fd = unix_connect(tap_dev);
960
961                 if (tap_fd < 0) {
962                         warn("Unable to open %s", tap_dev);
963                         return -1;
964                 }
965         }
966
967         /*
968          * Check whether the device file is a tap(4)
969          */
970         if (fstat(tap_fd, &st) < 0) {
971                 failed = 1;
972         } else if (S_ISCHR(st.st_mode)) {
973                 dname = fdevname(tap_fd);
974                 if (dname)
975                         dname = strstr(dname, "tap");
976                 if (dname) {
977                         /*
978                          * Bring up the corresponding tap(4) interface
979                          */
980                         *tap_unit = strtol(dname + 3, NULL, 10);
981                         printf("TAP UNIT %d\n", *tap_unit);
982                         if (netif_set_tapflags(*tap_unit, IFF_UP, s) == 0)
983                                 failed = 0;
984                         else
985                                 failed = 1;
986                 } else {
987                         failed = 1;
988                 }
989         } else if (S_ISSOCK(st.st_mode)) {
990                 /*
991                  * Special socket connection (typically to vknet).  We
992                  * do not have to do anything.
993                  */
994                 failed = 0;
995         } else {
996                 failed = 1;
997         }
998
999         if (failed) {
1000                 warnx("%s is not a tap(4) device or socket", tap_dev);
1001                 close(tap_fd);
1002                 tap_fd = -1;
1003                 *tap_unit = -1;
1004         }
1005         return tap_fd;
1006 }
1007
1008 static int
1009 unix_connect(const char *path)
1010 {
1011         struct sockaddr_un sunx;
1012         int len;
1013         int net_fd;
1014         int sndbuf = 262144;
1015         struct stat st;
1016
1017         snprintf(sunx.sun_path, sizeof(sunx.sun_path), "%s", path);
1018         len = offsetof(struct sockaddr_un, sun_path[strlen(sunx.sun_path)]);
1019         ++len;  /* include nul */
1020         sunx.sun_family = AF_UNIX;
1021         sunx.sun_len = len;
1022
1023         net_fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
1024         if (net_fd < 0)
1025                 return(-1);
1026         if (connect(net_fd, (void *)&sunx, len) < 0) {
1027                 close(net_fd);
1028                 return(-1);
1029         }
1030         setsockopt(net_fd, SOL_SOCKET, SO_SNDBUF, &sndbuf, sizeof(sndbuf));
1031         if (fstat(net_fd, &st) == 0)
1032                 printf("Network socket buffer: %d bytes\n", st.st_blksize);
1033         fcntl(net_fd, F_SETFL, O_NONBLOCK);
1034         return(net_fd);
1035 }
1036
1037 #undef TAPDEV_MAJOR
1038 #undef TAPDEV_MINOR
1039 #undef TAPDEV_OFLAGS
1040
1041 /*
1042  * Following syntax is supported,
1043  * 1) x.x.x.x             tap(4)'s address is x.x.x.x
1044  *
1045  * 2) x.x.x.x/z           tap(4)'s address is x.x.x.x
1046  *                        tap(4)'s netmask len is z
1047  *
1048  * 3) x.x.x.x:y.y.y.y     tap(4)'s address is x.x.x.x
1049  *                        pseudo netif's address is y.y.y.y
1050  *
1051  * 4) x.x.x.x:y.y.y.y/z   tap(4)'s address is x.x.x.x
1052  *                        pseudo netif's address is y.y.y.y
1053  *                        tap(4) and pseudo netif's netmask len are z
1054  *
1055  * 5) bridgeX             tap(4) will be added to bridgeX
1056  *
1057  * 6) bridgeX:y.y.y.y     tap(4) will be added to bridgeX
1058  *                        pseudo netif's address is y.y.y.y
1059  *
1060  * 7) bridgeX:y.y.y.y/z   tap(4) will be added to bridgeX
1061  *                        pseudo netif's address is y.y.y.y
1062  *                        pseudo netif's netmask len is z
1063  */
1064 static
1065 int
1066 netif_init_tap(int tap_unit, in_addr_t *addr, in_addr_t *mask, int s)
1067 {
1068         in_addr_t tap_addr, netmask, netif_addr;
1069         int next_netif_addr;
1070         char *tok, *masklen_str, *ifbridge;
1071
1072         *addr = 0;
1073         *mask = 0;
1074
1075         tok = strtok(NULL, ":/");
1076         if (tok == NULL) {
1077                 /*
1078                  * Nothing special, simply use tap(4) as backend
1079                  */
1080                 return 0;
1081         }
1082
1083         if (inet_pton(AF_INET, tok, &tap_addr) > 0) {
1084                 /*
1085                  * tap(4)'s address is supplied
1086                  */
1087                 ifbridge = NULL;
1088
1089                 /*
1090                  * If there is next token, then it may be pseudo
1091                  * netif's address or netmask len for tap(4)
1092                  */
1093                 next_netif_addr = 0;
1094         } else {
1095                 /*
1096                  * Not tap(4)'s address, assume it as a bridge(4)
1097                  * iface name
1098                  */
1099                 tap_addr = 0;
1100                 ifbridge = tok;
1101
1102                 /*
1103                  * If there is next token, then it must be pseudo
1104                  * netif's address
1105                  */
1106                 next_netif_addr = 1;
1107         }
1108
1109         netmask = netif_addr = 0;
1110
1111         tok = strtok(NULL, ":/");
1112         if (tok == NULL)
1113                 goto back;
1114
1115         if (inet_pton(AF_INET, tok, &netif_addr) <= 0) {
1116                 if (next_netif_addr) {
1117                         warnx("Invalid pseudo netif address: %s", tok);
1118                         return -1;
1119                 }
1120                 netif_addr = 0;
1121
1122                 /*
1123                  * Current token is not address, then it must be netmask len
1124                  */
1125                 masklen_str = tok;
1126         } else {
1127                 /*
1128                  * Current token is pseudo netif address, if there is next token
1129                  * it must be netmask len
1130                  */
1131                 masklen_str = strtok(NULL, "/");
1132         }
1133
1134         /* Calculate netmask */
1135         if (masklen_str != NULL) {
1136                 u_long masklen;
1137
1138                 masklen = strtoul(masklen_str, NULL, 10);
1139                 if (masklen < 32 && masklen > 0) {
1140                         netmask = htonl(~((1LL << (32 - masklen)) - 1)
1141                                         & 0xffffffff);
1142                 } else {
1143                         warnx("Invalid netmask len: %lu", masklen);
1144                         return -1;
1145                 }
1146         }
1147
1148         /* Make sure there is no more token left */
1149         if (strtok(NULL, ":/") != NULL) {
1150                 warnx("Invalid argument to '-I'");
1151                 return -1;
1152         }
1153
1154 back:
1155         if (tap_unit < 0) {
1156                 /* Do nothing */
1157         } else if (ifbridge == NULL) {
1158                 /* Set tap(4) address/netmask */
1159                 if (netif_set_tapaddr(tap_unit, tap_addr, netmask, s) < 0)
1160                         return -1;
1161         } else {
1162                 /* Tie tap(4) to bridge(4) */
1163                 if (netif_add_tap2brg(tap_unit, ifbridge, s) < 0)
1164                         return -1;
1165         }
1166
1167         *addr = netif_addr;
1168         *mask = netmask;
1169         return 0;
1170 }
1171
1172 /*
1173  * NetifInfo[] will be filled for pseudo netif initialization.
1174  * NetifNum will be bumped to reflect the number of valid entries
1175  * in NetifInfo[].
1176  */
1177 static
1178 void
1179 init_netif(char *netifExp[], int netifExpNum)
1180 {
1181         int i, s;
1182         char *tmp;
1183
1184         if (netifExpNum == 0)
1185                 return;
1186
1187         s = socket(AF_INET, SOCK_DGRAM, 0);     /* for ioctl(SIOC) */
1188         if (s < 0)
1189                 return;
1190
1191         for (i = 0; i < netifExpNum; ++i) {
1192                 struct vknetif_info *info;
1193                 in_addr_t netif_addr, netif_mask;
1194                 int tap_fd, tap_unit;
1195                 char *netif;
1196
1197                 /* Extract MAC address if there is one */
1198                 tmp = netifExp[i];
1199                 strsep(&tmp, "=");
1200
1201                 netif = strtok(netifExp[i], ":");
1202                 if (netif == NULL) {
1203                         warnx("Invalid argument to '-I'");
1204                         continue;
1205                 }
1206
1207                 /*
1208                  * Open tap(4) device file and bring up the
1209                  * corresponding interface
1210                  */
1211                 tap_fd = netif_open_tap(netif, &tap_unit, s);
1212                 if (tap_fd < 0)
1213                         continue;
1214
1215                 /*
1216                  * Initialize tap(4) and get address/netmask
1217                  * for pseudo netif
1218                  *
1219                  * NB: Rest part of netifExp[i] is passed
1220                  *     to netif_init_tap() implicitly.
1221                  */
1222                 if (netif_init_tap(tap_unit, &netif_addr, &netif_mask, s) < 0) {
1223                         /*
1224                          * NB: Closing tap(4) device file will bring
1225                          *     down the corresponding interface
1226                          */
1227                         close(tap_fd);
1228                         continue;
1229                 }
1230
1231                 info = &NetifInfo[NetifNum];
1232                 bzero(info, sizeof(*info));
1233                 info->tap_fd = tap_fd;
1234                 info->tap_unit = tap_unit;
1235                 info->netif_addr = netif_addr;
1236                 info->netif_mask = netif_mask;
1237                 /*
1238                  * If tmp isn't NULL it means a MAC could have been
1239                  * specified so attempt to convert it.
1240                  * Setting enaddr to NULL will tell vke_attach() we
1241                  * need a pseudo-random MAC address.
1242                  */
1243                 if (tmp != NULL) {
1244                         if ((info->enaddr = malloc(ETHER_ADDR_LEN)) == NULL)
1245                                 warnx("Couldn't allocate memory for the operation");
1246                         else {
1247                                 if ((kether_aton(tmp, info->enaddr)) == NULL) {
1248                                         free(info->enaddr);
1249                                         info->enaddr = NULL;
1250                                 }
1251                         }
1252                 }
1253
1254                 NetifNum++;
1255                 if (NetifNum >= VKNETIF_MAX)    /* XXX will this happen? */
1256                         break;
1257         }
1258         close(s);
1259 }
1260
1261 /*
1262  * Create the pid file and leave it open and locked while the vkernel is
1263  * running.  This allows a script to use /usr/bin/lockf to probe whether
1264  * a vkernel is still running (so as not to accidently kill an unrelated
1265  * process from a stale pid file).
1266  */
1267 static
1268 void
1269 writepid(void)
1270 {
1271         char buf[32];
1272         int fd;
1273
1274         if (pid_file != NULL) {
1275                 snprintf(buf, sizeof(buf), "%ld\n", (long)getpid());
1276                 fd = open(pid_file, O_RDWR|O_CREAT|O_EXLOCK|O_NONBLOCK, 0666);
1277                 if (fd < 0) {
1278                         if (errno == EWOULDBLOCK) {
1279                                 perror("Failed to lock pidfile, "
1280                                        "vkernel already running");
1281                         } else {
1282                                 perror("Failed to create pidfile");
1283                         }
1284                         exit(EX_SOFTWARE);
1285                 }
1286                 ftruncate(fd, 0);
1287                 write(fd, buf, strlen(buf));
1288                 /* leave the file open to maintain the lock */
1289         }
1290 }
1291
1292 static
1293 void
1294 cleanpid( void )
1295 {
1296         if (pid_file != NULL) {
1297                 if (unlink(pid_file) < 0)
1298                         perror("Warning: couldn't remove pidfile");
1299         }
1300 }
1301
1302 static
1303 void
1304 usage_err(const char *ctl, ...)
1305 {
1306         va_list va;
1307
1308         va_start(va, ctl);
1309         vfprintf(stderr, ctl, va);
1310         va_end(va);
1311         fprintf(stderr, "\n");
1312         exit(EX_USAGE);
1313 }
1314
1315 static
1316 void
1317 usage_help(_Bool help)
1318 {
1319         fprintf(stderr, "Usage: %s [-hsUv] [-c file] [-e name=value:name=value:...]\n"
1320             "\t[-i file] [-I interface[:address1[:address2][/netmask]]] [-l cpulock]\n"
1321             "\t[-m size] [-n numcpus[:lbits[:cbits]]]\n"
1322             "\t[-p file] [-r file]\n", save_av[0]);
1323
1324         if (help)
1325                 fprintf(stderr, "\nArguments:\n"
1326                     "\t-c\tSpecify a readonly CD-ROM image file to be used by the kernel.\n"
1327                     "\t-e\tSpecify an environment to be used by the kernel.\n"
1328                     "\t-h\tThis list of options.\n"
1329                     "\t-i\tSpecify a memory image file to be used by the virtual kernel.\n"
1330                     "\t-I\tCreate a virtual network device.\n"
1331                     "\t-l\tSpecify which, if any, real CPUs to lock virtual CPUs to.\n"
1332                     "\t-m\tSpecify the amount of memory to be used by the kernel in bytes.\n"
1333                     "\t-n\tSpecify the number of CPUs and the topology you wish to emulate:\n"
1334                     "\t  \t- numcpus - number of cpus\n"
1335                     "\t  \t- :lbits - specify the number of bits within APICID(=CPUID) needed for representing\n"
1336                     "\t  \t  the logical ID. Controls the number of threads/core (0bits - 1 thread, 1bit - 2 threads).\n"
1337                     "\t  \t- :cbits - specify the number of bits within APICID(=CPUID) needed for representing\n"
1338                     "\t  \t  the core ID. Controls the number of core/package (0bits - 1 core, 1bit - 2 cores).\n"
1339                     "\t-p\tSpecify a file in which to store the process ID.\n"
1340                     "\t-r\tSpecify a R/W disk image file to be used by the kernel.\n"
1341                     "\t-s\tBoot into single-user mode.\n"
1342                     "\t-U\tEnable writing to kernel memory and module loading.\n"
1343                     "\t-v\tTurn on verbose booting.\n");
1344
1345         exit(EX_USAGE);
1346 }
1347
1348 void
1349 cpu_reset(void)
1350 {
1351         kprintf("cpu reset, rebooting vkernel\n");
1352         closefrom(3);
1353         cleanpid();
1354         execv(save_av[0], save_av);
1355 }
1356
1357 void
1358 cpu_halt(void)
1359 {
1360         kprintf("cpu halt, exiting vkernel\n");
1361         cleanpid();
1362         exit(EX_OK);
1363 }
1364
1365 void
1366 setrealcpu(void)
1367 {
1368         switch(lwp_cpu_lock) {
1369         case LCL_PER_CPU:
1370                 if (bootverbose)
1371                         kprintf("Locking CPU%d to real cpu %d\n",
1372                                 mycpuid, next_cpu);
1373                 usched_set(getpid(), USCHED_SET_CPU, &next_cpu, sizeof(next_cpu));
1374                 next_cpu++;
1375                 if (next_cpu >= real_ncpus)
1376                         next_cpu = 0;
1377                 break;
1378         case LCL_SINGLE_CPU:
1379                 if (bootverbose)
1380                         kprintf("Locking CPU%d to real cpu %d\n",
1381                                 mycpuid, next_cpu);
1382                 usched_set(getpid(), USCHED_SET_CPU, &next_cpu, sizeof(next_cpu));
1383                 break;
1384         default:
1385                 /* do not map virtual cpus to real cpus */
1386                 break;
1387         }
1388 }
1389
1390 /*
1391  * Allocate and free memory for module loading.  The loaded module
1392  * has to be placed somewhere near the current kernel binary load
1393  * point or the relocations will not work.
1394  *
1395  * I'm not sure why this isn't working.
1396  */
1397 int
1398 vkernel_module_memory_alloc(vm_offset_t *basep, size_t bytes)
1399 {
1400         kprintf("module loading for vkernel64's not currently supported\n");
1401         *basep = 0;
1402         return ENOMEM;
1403 #if 0
1404 #if 1
1405         size_t xtra;
1406         xtra = (PAGE_SIZE - (vm_offset_t)sbrk(0)) & PAGE_MASK;
1407         *basep = (vm_offset_t)sbrk(xtra + bytes) + xtra;
1408         bzero((void *)*basep, bytes);
1409 #else
1410         *basep = (vm_offset_t)mmap((void *)0x000000000, bytes,
1411                                    PROT_READ|PROT_WRITE|PROT_EXEC,
1412                                    MAP_ANON|MAP_SHARED, -1, 0);
1413         if ((void *)*basep == MAP_FAILED)
1414                 return ENOMEM;
1415 #endif
1416         kprintf("basep %p %p %zd\n",
1417                 (void *)vkernel_module_memory_alloc, (void *)*basep, bytes);
1418         return 0;
1419 #endif
1420 }
1421
1422 void
1423 vkernel_module_memory_free(vm_offset_t base, size_t bytes)
1424 {
1425 #if 0
1426 #if 0
1427         munmap((void *)base, bytes);
1428 #endif
1429 #endif
1430 }