xref: /f-stack/lib/ff_freebsd_init.c (revision a9643ea8)
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 
40 #include <vm/uma.h>
41 #include <vm/uma_int.h>
42 #include <vm/vm.h>
43 #include <vm/vm_extern.h>
44 
45 #include "ff_host_interface.h"
46 #include "ff_api.h"
47 #include "ff_config.h"
48 
49 unsigned int sleep(unsigned int seconds);
50 
51 int ff_freebsd_init(void);
52 
53 extern void mutex_init(void);
54 extern void mi_startup(void);
55 extern void uma_startup(void *, int);
56 extern void uma_startup2(void);
57 
58 extern void ff_init_thread0(void);
59 
60 struct sx proctree_lock;
61 struct pcpu *pcpup;
62 struct uma_page_head *uma_page_slab_hash;
63 int uma_page_mask;
64 extern cpuset_t all_cpus;
65 
66 long physmem;
67 
68 int
69 ff_freebsd_init(void)
70 {
71     int boot_pages;
72     unsigned int num_hash_buckets;
73     char tmpbuf[32] = {0};
74     void *bootmem;
75     int error;
76 
77     snprintf(tmpbuf, sizeof(tmpbuf), "%u", ff_global_cfg.freebsd.hz);
78     error = kern_setenv("kern.hz", tmpbuf);
79     if (error != 0) {
80         panic("kern_setenv failed: kern.hz=%s\n", tmpbuf);
81     }
82 
83     struct ff_freebsd_cfg *cur;
84     cur = ff_global_cfg.freebsd.boot;
85     while (cur) {
86         error = kern_setenv(cur->name, cur->str);
87         if (error != 0) {
88             printf("kern_setenv failed: %s=%s\n", cur->name, cur->str);
89         }
90 
91         cur = cur->next;
92     }
93 
94     physmem = ff_global_cfg.freebsd.physmem;
95 
96     pcpup = malloc(sizeof(struct pcpu), M_DEVBUF, M_ZERO);
97     pcpu_init(pcpup, 0, sizeof(struct pcpu));
98     CPU_SET(0, &all_cpus);
99 
100     ff_init_thread0();
101 
102     boot_pages = 16;
103     bootmem = (void *)kmem_malloc(NULL, boot_pages*PAGE_SIZE, M_ZERO);
104     uma_startup(bootmem, boot_pages);
105     uma_startup2();
106 
107     num_hash_buckets = 8192;
108     uma_page_slab_hash = (struct uma_page_head *)kmem_malloc(NULL, sizeof(struct uma_page)*num_hash_buckets, M_ZERO);
109     uma_page_mask = num_hash_buckets - 1;
110 
111     mutex_init();
112     mi_startup();
113     sx_init(&proctree_lock, "proctree");
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\n", cur->name, cur->str);
122         }
123 
124         cur = cur->next;
125     }
126 
127     return (0);
128 }
129