xref: /f-stack/lib/ff_freebsd_init.c (revision 8cf1d457)
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 int
68 ff_freebsd_init(void)
69 {
70     int boot_pages;
71     unsigned int num_hash_buckets;
72     char tmpbuf[32] = {0};
73     void *bootmem;
74     int error;
75 
76     snprintf(tmpbuf, sizeof(tmpbuf), "%u", ff_global_cfg.freebsd.hz);
77     error = kern_setenv("kern.hz", tmpbuf);
78     if (error != 0) {
79         panic("kern_setenv failed: kern.hz=%s\n", tmpbuf);
80     }
81 
82     struct ff_freebsd_cfg *cur;
83     cur = ff_global_cfg.freebsd.boot;
84     while (cur) {
85         error = kern_setenv(cur->name, cur->str);
86         if (error != 0) {
87             printf("kern_setenv failed: %s=%s\n", cur->name, cur->str);
88         }
89 
90         cur = cur->next;
91     }
92 
93     physmem = ff_global_cfg.freebsd.physmem;
94 
95     pcpup = malloc(sizeof(struct pcpu), M_DEVBUF, M_ZERO);
96     pcpu_init(pcpup, 0, sizeof(struct pcpu));
97     CPU_SET(0, &all_cpus);
98 
99     ff_init_thread0();
100 
101     boot_pages = 16;
102     bootmem = (void *)kmem_malloc(NULL, boot_pages*PAGE_SIZE, M_ZERO);
103     uma_startup(bootmem, boot_pages);
104     uma_startup2();
105 
106     num_hash_buckets = 8192;
107     uma_page_slab_hash = (struct uma_page_head *)kmem_malloc(NULL, sizeof(struct uma_page)*num_hash_buckets, M_ZERO);
108     uma_page_mask = num_hash_buckets - 1;
109 
110     mutex_init();
111     mi_startup();
112     sx_init(&proctree_lock, "proctree");
113     ff_fdused_range(ff_global_cfg.freebsd.fd_reserve);
114 
115     cur = ff_global_cfg.freebsd.sysctl;
116     while (cur) {
117         error = kernel_sysctlbyname(curthread, cur->name, NULL, NULL,
118             cur->value, cur->vlen, NULL, 0);
119 
120         if (error != 0) {
121             printf("kernel_sysctlbyname failed: %s=%s, error:%d\n",
122                 cur->name, cur->str, error);
123         }
124 
125         cur = cur->next;
126     }
127 
128     return (0);
129 }
130