1 /* 2 * Copyright (c) 2010 Kip Macy All rights reserved. 3 * Copyright (C) 2017 THL A29 Limited, a Tencent company. 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright notice, this 10 * list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 20 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 21 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 22 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 24 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 * 26 * Derived in part from libplebnet's pn_init.c. 27 */ 28 29 #include <sys/cdefs.h> 30 #include <sys/param.h> 31 #include <sys/pcpu.h> 32 #include <sys/systm.h> 33 #include <sys/proc.h> 34 #include <sys/lock.h> 35 #include <sys/sx.h> 36 #include <sys/vmmeter.h> 37 #include <sys/cpuset.h> 38 #include <sys/sysctl.h> 39 #include <sys/filedesc.h> 40 41 #include <vm/uma.h> 42 #include <vm/uma_int.h> 43 #include <vm/vm.h> 44 #include <vm/vm_extern.h> 45 46 #include "ff_host_interface.h" 47 #include "ff_api.h" 48 #include "ff_config.h" 49 50 int ff_freebsd_init(void); 51 52 extern void mutex_init(void); 53 extern void mi_startup(void); 54 extern void uma_startup(void *, int); 55 extern void uma_startup2(void); 56 57 extern void ff_init_thread0(void); 58 59 struct sx proctree_lock; 60 struct pcpu *pcpup; 61 struct uma_page_head *uma_page_slab_hash; 62 int uma_page_mask; 63 extern cpuset_t all_cpus; 64 65 long physmem; 66 67 extern void uma_startup1(vm_offset_t); 68 69 int 70 ff_freebsd_init(void) 71 { 72 int boot_pages; 73 unsigned int num_hash_buckets; 74 char tmpbuf[32] = {0}; 75 void *bootmem; 76 int error; 77 78 snprintf(tmpbuf, sizeof(tmpbuf), "%u", ff_global_cfg.freebsd.hz); 79 error = kern_setenv("kern.hz", tmpbuf); 80 if (error != 0) { 81 panic("kern_setenv failed: kern.hz=%s\n", tmpbuf); 82 } 83 84 struct ff_freebsd_cfg *cur; 85 cur = ff_global_cfg.freebsd.boot; 86 while (cur) { 87 error = kern_setenv(cur->name, cur->str); 88 if (error != 0) { 89 printf("kern_setenv failed: %s=%s\n", cur->name, cur->str); 90 } 91 92 cur = cur->next; 93 } 94 95 physmem = ff_global_cfg.freebsd.physmem; 96 97 pcpup = malloc(sizeof(struct pcpu), M_DEVBUF, M_ZERO); 98 pcpu_init(pcpup, 0, sizeof(struct pcpu)); 99 CPU_SET(0, &all_cpus); 100 101 ff_init_thread0(); 102 103 boot_pages = 16; 104 bootmem = (void *)kmem_malloc(boot_pages*PAGE_SIZE, M_ZERO); 105 //uma_startup(bootmem, boot_pages); 106 uma_startup1((vm_offset_t)bootmem); 107 uma_startup2(); 108 109 num_hash_buckets = 8192; 110 uma_page_slab_hash = (struct uma_page_head *)kmem_malloc(sizeof(struct uma_page)*num_hash_buckets, M_ZERO); 111 uma_page_mask = num_hash_buckets - 1; 112 113 mutex_init(); 114 mi_startup(); 115 sx_init(&proctree_lock, "proctree"); 116 ff_fdused_range(ff_global_cfg.freebsd.fd_reserve); 117 118 cur = ff_global_cfg.freebsd.sysctl; 119 while (cur) { 120 error = kernel_sysctlbyname(curthread, cur->name, NULL, NULL, 121 cur->value, cur->vlen, NULL, 0); 122 123 if (error != 0) { 124 printf("kernel_sysctlbyname failed: %s=%s, error:%d\n", 125 cur->name, cur->str, error); 126 } 127 128 cur = cur->next; 129 } 130 131 return (0); 132 } 133