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