1 /*
2 * Copyright (c) 2000-2021 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29 #include <kern/kalloc.h>
30 #include <libkern/OSAtomic.h>
31 #include <sys/errno.h>
32 #include <sys/sysctl.h>
33 #include <net/init.h>
34 #include <libkern/libkern.h>
35 #include <os/atomic_private.h>
36 #include <pexpert/pexpert.h>
37 #include <string.h>
38
39 #if (DEBUG | DEVELOPMENT)
40 SYSCTL_DECL(_net_diagnose);
41 SYSCTL_NODE(_net, OID_AUTO, diagnose, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "");
42
43 static int net_diagnose_on = 0;
44 SYSCTL_INT(_net_diagnose, OID_AUTO, on,
45 CTLFLAG_RW | CTLFLAG_LOCKED, &net_diagnose_on, 0, "");
46 #endif /* (DEBUG | DEVELOPMENT) */
47
48 struct init_list_entry {
49 struct init_list_entry *next;
50 net_init_func_ptr func;
51 };
52
53 static struct init_list_entry *LIST_RAN = __unsafe_forge_single(struct init_list_entry *, ~(uintptr_t)0);
54 static struct init_list_entry *list_head = NULL;
55
56 errno_t
net_init_add(net_init_func_ptr init_func)57 net_init_add(net_init_func_ptr init_func)
58 {
59 struct init_list_entry *entry;
60
61 if (init_func == 0) {
62 return EINVAL;
63 }
64
65 /* Check if we've already started */
66 if (list_head == LIST_RAN) {
67 return EALREADY;
68 }
69
70 entry = kalloc_type(struct init_list_entry,
71 Z_WAITOK | Z_ZERO | Z_NOFAIL);
72
73 entry->func = init_func;
74
75 do {
76 entry->next = list_head;
77
78 if (entry->next == LIST_RAN) {
79 /* List already ran, cleanup and call the function */
80 kfree_type(struct init_list_entry, entry);
81 return EALREADY;
82 }
83 } while (!os_atomic_cmpxchg(&list_head, entry->next, entry, acq_rel));
84
85 return 0;
86 }
87
88 __private_extern__ void
net_init_run(void)89 net_init_run(void)
90 {
91 struct init_list_entry *__single backward_head = NULL;
92 struct init_list_entry *__single forward_head = NULL;
93 struct init_list_entry *__single current = NULL;
94
95 /*
96 * Grab the list, replacing the head with 0xffffffff to indicate
97 * that we've already run.
98 */
99 do {
100 backward_head = list_head;
101 } while (!os_atomic_cmpxchg(&list_head, backward_head, LIST_RAN, acq_rel));
102
103 /* Reverse the order of the list */
104 while (backward_head != 0) {
105 current = backward_head;
106 backward_head = current->next;
107 current->next = forward_head;
108 forward_head = current;
109 }
110
111 /* Call each function pointer registered */
112 while (forward_head != 0) {
113 current = forward_head;
114 forward_head = current->next;
115 current->func();
116 kfree_type(struct init_list_entry, current);
117 }
118
119 #if (DEBUG || DEVELOPMENT)
120 (void) PE_parse_boot_argn("net_diagnose_on", &net_diagnose_on, sizeof(net_diagnose_on));
121 #endif /* (DEBUG || DEVELOPMENT) */
122 }
123