xref: /f-stack/lib/ff_freebsd_init.c (revision 615f2d3c)
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 unsigned int sleep(unsigned int seconds);
51 
52 int ff_freebsd_init(void);
53 
54 extern void mutex_init(void);
55 extern void mi_startup(void);
56 extern void uma_startup(void *, int);
57 extern void uma_startup2(void);
58 
59 extern void ff_init_thread0(void);
60 
61 struct sx proctree_lock;
62 struct pcpu *pcpup;
63 struct uma_page_head *uma_page_slab_hash;
64 int uma_page_mask;
65 extern cpuset_t all_cpus;
66 
67 long physmem;
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(NULL, boot_pages*PAGE_SIZE, M_ZERO);
105     uma_startup(bootmem, boot_pages);
106     uma_startup2();
107 
108     num_hash_buckets = 8192;
109     uma_page_slab_hash = (struct uma_page_head *)kmem_malloc(NULL, sizeof(struct uma_page)*num_hash_buckets, M_ZERO);
110     uma_page_mask = num_hash_buckets - 1;
111 
112     mutex_init();
113     mi_startup();
114     sx_init(&proctree_lock, "proctree");
115     ff_fdused_range(ff_global_cfg.freebsd.fd_reserve);
116 
117     cur = ff_global_cfg.freebsd.sysctl;
118     while (cur) {
119         error = kernel_sysctlbyname(curthread, cur->name, NULL, NULL,
120             cur->value, cur->vlen, NULL, 0);
121 
122         if (error != 0) {
123             printf("kernel_sysctlbyname failed: %s=%s\n", cur->name, cur->str);
124         }
125 
126         cur = cur->next;
127     }
128 
129     return (0);
130 }
131