xref: /f-stack/lib/ff_freebsd_init.c (revision 2317ada5)
1 /*
2  * Copyright (c) 2010 Kip Macy All rights reserved.
3  * Copyright (C) 2017-2021 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
ff_freebsd_init(void)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     PCPU_SET(prvspace, pcpup);
100     CPU_SET(0, &all_cpus);
101 
102     ff_init_thread0();
103 
104     boot_pages = 16;
105     bootmem = (void *)kmem_malloc(boot_pages*PAGE_SIZE, M_ZERO);
106     //uma_startup(bootmem, boot_pages);
107     uma_startup1((vm_offset_t)bootmem);
108     uma_startup2();
109 
110     num_hash_buckets = 8192;
111     uma_page_slab_hash = (struct uma_page_head *)kmem_malloc(sizeof(struct uma_page)*num_hash_buckets, M_ZERO);
112     uma_page_mask = num_hash_buckets - 1;
113 
114     mutex_init();
115     mi_startup();
116     sx_init(&proctree_lock, "proctree");
117     ff_fdused_range(ff_global_cfg.freebsd.fd_reserve);
118 
119     cur = ff_global_cfg.freebsd.sysctl;
120     while (cur) {
121         error = kernel_sysctlbyname(curthread, cur->name, NULL, NULL,
122             cur->value, cur->vlen, NULL, 0);
123 
124         if (error != 0) {
125             printf("kernel_sysctlbyname failed: %s=%s, error:%d\n",
126                 cur->name, cur->str, error);
127         }
128 
129         cur = cur->next;
130     }
131 
132     return (0);
133 }
134