xref: /xnu-8796.101.5/osfmk/kern/mach_node.h (revision aca3beaa3dfbd42498b42c5e5ce20a938e6554e5)
1*aca3beaaSApple OSS Distributions /*
2*aca3beaaSApple OSS Distributions  * Copyright (c) 2015-2016 Apple Computer, Inc. All rights reserved.
3*aca3beaaSApple OSS Distributions  *
4*aca3beaaSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*aca3beaaSApple OSS Distributions  *
6*aca3beaaSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*aca3beaaSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*aca3beaaSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*aca3beaaSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*aca3beaaSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*aca3beaaSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*aca3beaaSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*aca3beaaSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*aca3beaaSApple OSS Distributions  *
15*aca3beaaSApple OSS Distributions  * Please obtain a copy of the License at
16*aca3beaaSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*aca3beaaSApple OSS Distributions  *
18*aca3beaaSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*aca3beaaSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*aca3beaaSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*aca3beaaSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*aca3beaaSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*aca3beaaSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*aca3beaaSApple OSS Distributions  * limitations under the License.
25*aca3beaaSApple OSS Distributions  *
26*aca3beaaSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*aca3beaaSApple OSS Distributions  */
28*aca3beaaSApple OSS Distributions /*
29*aca3beaaSApple OSS Distributions  *  File:   kern/mach_node.h
30*aca3beaaSApple OSS Distributions  *  Author: Dean Reece
31*aca3beaaSApple OSS Distributions  *  Date:   2016
32*aca3beaaSApple OSS Distributions  *
33*aca3beaaSApple OSS Distributions  *  Definitions for mach internode communication (used by flipc).
34*aca3beaaSApple OSS Distributions  *  This header is intended for use inside the kernel only.
35*aca3beaaSApple OSS Distributions  */
36*aca3beaaSApple OSS Distributions 
37*aca3beaaSApple OSS Distributions #ifndef _KERN_MACH_NODE_H_
38*aca3beaaSApple OSS Distributions #define _KERN_MACH_NODE_H_
39*aca3beaaSApple OSS Distributions 
40*aca3beaaSApple OSS Distributions #if defined(MACH_KERNEL_PRIVATE) || defined(__APPLE_API_PRIVATE)
41*aca3beaaSApple OSS Distributions 
42*aca3beaaSApple OSS Distributions /*** Mach Node Name Server Section
43*aca3beaaSApple OSS Distributions  *  Definitions shared by the mach_node layer in the kernel and the
44*aca3beaaSApple OSS Distributions  *  node's bootstrap server (noded).
45*aca3beaaSApple OSS Distributions  */
46*aca3beaaSApple OSS Distributions 
47*aca3beaaSApple OSS Distributions /* This structure describes messages sent from the mach_node layer to the
48*aca3beaaSApple OSS Distributions  * node bootstrap server.
49*aca3beaaSApple OSS Distributions  */
50*aca3beaaSApple OSS Distributions #pragma pack(4)
51*aca3beaaSApple OSS Distributions typedef struct mach_node_server_msg {
52*aca3beaaSApple OSS Distributions 	mach_msg_header_t   header;
53*aca3beaaSApple OSS Distributions 	uint32_t    identifier; // See FLIPC_SM_* defines
54*aca3beaaSApple OSS Distributions 	uint32_t    options;    // Currently unused
55*aca3beaaSApple OSS Distributions 	uint32_t    node_id;    // Node number
56*aca3beaaSApple OSS Distributions } *mach_node_server_msg_t;
57*aca3beaaSApple OSS Distributions #pragma pack()
58*aca3beaaSApple OSS Distributions 
59*aca3beaaSApple OSS Distributions /* This structure describes node registration messages sent from the mach_node
60*aca3beaaSApple OSS Distributions  * layer to the node bootstrap server.
61*aca3beaaSApple OSS Distributions  */
62*aca3beaaSApple OSS Distributions typedef struct mach_node_server_register_msg {
63*aca3beaaSApple OSS Distributions 	struct mach_node_server_msg node_header;
64*aca3beaaSApple OSS Distributions 	uint8_t     datamodel;  // 1==ILP32, 2==LP64; matches dtrace
65*aca3beaaSApple OSS Distributions 	uint8_t     byteorder;  // Uses defines from libkern/OSByteOrder.h
66*aca3beaaSApple OSS Distributions } *mach_node_server_register_msg_t;
67*aca3beaaSApple OSS Distributions #pragma pack()
68*aca3beaaSApple OSS Distributions 
69*aca3beaaSApple OSS Distributions #define MACH_NODE_SERVER_MSG_ID (0x45444f4eUL)  // msgh_id "NODE" for Node msgs
70*aca3beaaSApple OSS Distributions #define MACH_NODE_SM_REG_LOCAL           (0UL)  // Register the local node
71*aca3beaaSApple OSS Distributions #define MACH_NODE_SM_REG_REMOTE          (1UL)  // Register a remote node
72*aca3beaaSApple OSS Distributions 
73*aca3beaaSApple OSS Distributions #if defined(__LP64__)
74*aca3beaaSApple OSS Distributions #define LOCAL_DATA_MODEL    (2) // Native data model is LP64
75*aca3beaaSApple OSS Distributions #else
76*aca3beaaSApple OSS Distributions #define LOCAL_DATA_MODEL    (1) // Native data model is ILP32
77*aca3beaaSApple OSS Distributions #endif
78*aca3beaaSApple OSS Distributions 
79*aca3beaaSApple OSS Distributions #endif
80*aca3beaaSApple OSS Distributions 
81*aca3beaaSApple OSS Distributions 
82*aca3beaaSApple OSS Distributions #if MACH_FLIPC && defined(MACH_KERNEL_PRIVATE)
83*aca3beaaSApple OSS Distributions 
84*aca3beaaSApple OSS Distributions #include <kern/mach_node_link.h>
85*aca3beaaSApple OSS Distributions #include <kern/queue.h>
86*aca3beaaSApple OSS Distributions 
87*aca3beaaSApple OSS Distributions #include <sys/cdefs.h>
88*aca3beaaSApple OSS Distributions 
89*aca3beaaSApple OSS Distributions __BEGIN_DECLS
90*aca3beaaSApple OSS Distributions 
91*aca3beaaSApple OSS Distributions #define MACH_NODES_MAX          (2)         // Must be a power-of-2
92*aca3beaaSApple OSS Distributions #define MACH_NODE_ID_VALID(nid) (((nid) >= 0) && ((nid) < MACH_NODES_MAX))
93*aca3beaaSApple OSS Distributions 
94*aca3beaaSApple OSS Distributions typedef struct  flipc_node  *flipc_node_t;  // Defined in ipc/flipc.h
95*aca3beaaSApple OSS Distributions 
96*aca3beaaSApple OSS Distributions 
97*aca3beaaSApple OSS Distributions /*** Mach Node Section
98*aca3beaaSApple OSS Distributions  *
99*aca3beaaSApple OSS Distributions  *  An instance of mach_node is allocated for each node known to mach.
100*aca3beaaSApple OSS Distributions  *  In-kernel interfaces use a pointer to this structure to refer to a node.
101*aca3beaaSApple OSS Distributions  *  External interfaces and protocols refer to node by id (mach_node_id_t).
102*aca3beaaSApple OSS Distributions  */
103*aca3beaaSApple OSS Distributions typedef struct mach_node *mach_node_t;
104*aca3beaaSApple OSS Distributions 
105*aca3beaaSApple OSS Distributions struct mach_node {
106*aca3beaaSApple OSS Distributions 	/* Static node details, provided by the link driver at registration */
107*aca3beaaSApple OSS Distributions 	struct mnl_node_info info;
108*aca3beaaSApple OSS Distributions 
109*aca3beaaSApple OSS Distributions 	lck_spin_t      node_lock_data;
110*aca3beaaSApple OSS Distributions 
111*aca3beaaSApple OSS Distributions 	/* Flags and status word */
112*aca3beaaSApple OSS Distributions 	uint32_t        link:2;     // See MNL_LINK* defines
113*aca3beaaSApple OSS Distributions 	uint32_t        published:1;// True if node server has send-right
114*aca3beaaSApple OSS Distributions 	uint32_t        active:1;   // True if node is up and ready
115*aca3beaaSApple OSS Distributions 	uint32_t        suspended:1;// True if node is active but sleeping
116*aca3beaaSApple OSS Distributions 	uint32_t        dead:1;     // True if node is dead
117*aca3beaaSApple OSS Distributions 	uint32_t        _reserved:26;// Fill out the 32b flags field
118*aca3beaaSApple OSS Distributions 
119*aca3beaaSApple OSS Distributions 	/* port/space/set */
120*aca3beaaSApple OSS Distributions 	ipc_space_t     proxy_space;// Kernel special space for proxy rights
121*aca3beaaSApple OSS Distributions 	ipc_pset_t      proxy_port_set;// All proxy ports are in this set
122*aca3beaaSApple OSS Distributions 	ipc_port_t      bootstrap_port;// Port for which "noded" holds rcv right
123*aca3beaaSApple OSS Distributions 	ipc_port_t      control_port;// For control & ack/nak messages
124*aca3beaaSApple OSS Distributions 
125*aca3beaaSApple OSS Distributions 	/* Misc */
126*aca3beaaSApple OSS Distributions 	int             proto_vers; // Protocol version in use for this node
127*aca3beaaSApple OSS Distributions 	mach_node_t     antecedent; // Pointer to prior encarnation of this node id
128*aca3beaaSApple OSS Distributions };
129*aca3beaaSApple OSS Distributions 
130*aca3beaaSApple OSS Distributions extern mach_node_t  localnode;      // This node's mach_node_t struct
131*aca3beaaSApple OSS Distributions 
132*aca3beaaSApple OSS Distributions #define MACH_NODE_NULL              ((mach_node_t) 0UL)
133*aca3beaaSApple OSS Distributions #define MACH_NODE_SIZE              ((vm_offset_t)sizeof(struct mach_node))
134*aca3beaaSApple OSS Distributions #define MACH_NODE_VALID(node)       ((node) != MACH_NODE_NULL)
135*aca3beaaSApple OSS Distributions #define MACH_NODE_ALLOC()           ((mach_node_t)kalloc(MACH_NODE_SIZE))
136*aca3beaaSApple OSS Distributions #define MACH_NODE_FREE(node)        kfree(node, MACH_NODE_SIZE)
137*aca3beaaSApple OSS Distributions 
138*aca3beaaSApple OSS Distributions #define MACH_NODE_LOCK_INIT(np)     lck_spin_init(&(np)->node_lock_data, \
139*aca3beaaSApple OSS Distributions 	                                          &ipc_lck_grp, &ipc_lck_attr)
140*aca3beaaSApple OSS Distributions #define MACH_NODE_LOCK_DESTROY(np)  lck_spin_destroy(&(np)->node_lock_data, \
141*aca3beaaSApple OSS Distributions 	                                             &ipc_lck_grp)
142*aca3beaaSApple OSS Distributions #define MACH_NODE_LOCK(np)          lck_spin_lock(&(np)->node_lock_data)
143*aca3beaaSApple OSS Distributions #define MACH_NODE_UNLOCK(np)        lck_spin_unlock(&(np)->node_lock_data)
144*aca3beaaSApple OSS Distributions 
145*aca3beaaSApple OSS Distributions /*  Gets or allocates a locked mach_node struct for the specified <node_id>.
146*aca3beaaSApple OSS Distributions  *  The current node is locked and returned if it is not dead, or if it is dead
147*aca3beaaSApple OSS Distributions  *  and <alloc_if_dead> is false.  A new node struct is allocated, locked and
148*aca3beaaSApple OSS Distributions  *  returned if the node is dead and <alloc_if_dead> is true, or if the node
149*aca3beaaSApple OSS Distributions  *  is absent and <alloc_if_absent> is true.  MACH_NODE_NULL is returned if
150*aca3beaaSApple OSS Distributions  *  the node is absent and <alloc_if_absent> is false.  MACH_NODE_NULL is also
151*aca3beaaSApple OSS Distributions  *  returned if a new node structure was not able to be allocated.
152*aca3beaaSApple OSS Distributions  */
153*aca3beaaSApple OSS Distributions mach_node_t
154*aca3beaaSApple OSS Distributions mach_node_for_id_locked(mach_node_id_t  node_id,
155*aca3beaaSApple OSS Distributions     boolean_t       alloc_if_dead,
156*aca3beaaSApple OSS Distributions     boolean_t       alloc_if_absent);
157*aca3beaaSApple OSS Distributions 
158*aca3beaaSApple OSS Distributions 
159*aca3beaaSApple OSS Distributions /*** Mach Node Link Name Section
160*aca3beaaSApple OSS Distributions  *
161*aca3beaaSApple OSS Distributions  *  A node link name (mnl_name_t) is an oqaque value guaranteed unique across
162*aca3beaaSApple OSS Distributions  *  kernel instances on all nodes.  This guarantee requires that node ids not
163*aca3beaaSApple OSS Distributions  *  be recycled.
164*aca3beaaSApple OSS Distributions  *
165*aca3beaaSApple OSS Distributions  *  Names 0..(MACH_NODES_MAX-1) represent null (invalid) names
166*aca3beaaSApple OSS Distributions  *  Names MACH_NODES_MAX..(MACH_NODES_MAX*2-1) represent bootstrap names
167*aca3beaaSApple OSS Distributions  *  Names >=(MACH_NODES_MAX*2) represent normal names.
168*aca3beaaSApple OSS Distributions  */
169*aca3beaaSApple OSS Distributions 
170*aca3beaaSApple OSS Distributions /*  Allocate a new unique name and return it.
171*aca3beaaSApple OSS Distributions  *  Dispose of this with mnl_name_free().
172*aca3beaaSApple OSS Distributions  *  Returns MNL_NAME_NULL on failure.
173*aca3beaaSApple OSS Distributions  */
174*aca3beaaSApple OSS Distributions extern mnl_name_t   mnl_name_alloc(void);
175*aca3beaaSApple OSS Distributions 
176*aca3beaaSApple OSS Distributions /*  Deallocate a unique name that was allocated via mnl_name_alloc().
177*aca3beaaSApple OSS Distributions  */
178*aca3beaaSApple OSS Distributions extern void mnl_name_free(mnl_name_t name);
179*aca3beaaSApple OSS Distributions 
180*aca3beaaSApple OSS Distributions /*  This macro is used to convert a node id to a bootstrap port name.
181*aca3beaaSApple OSS Distributions  */
182*aca3beaaSApple OSS Distributions #define MNL_NAME_BOOTSTRAP(nid) ((mnl_name_t) MACH_NODES_MAX | (nid))
183*aca3beaaSApple OSS Distributions #define MNL_NAME_NULL ((mnl_name_t) 0UL)
184*aca3beaaSApple OSS Distributions #define MNL_NAME_VALID(obj) ((obj) >= MACH_NODES_MAX)
185*aca3beaaSApple OSS Distributions 
186*aca3beaaSApple OSS Distributions 
187*aca3beaaSApple OSS Distributions /*  The mnl hash table may optionally be used by clients to associate mnl_names
188*aca3beaaSApple OSS Distributions  *  with objects.  Objects to be stored in the hash table must start with an
189*aca3beaaSApple OSS Distributions  *  instance of struct mnk_obj.  It is up to clients of the hash table to
190*aca3beaaSApple OSS Distributions  *  allocate and free the actual objects being stored.
191*aca3beaaSApple OSS Distributions  */
192*aca3beaaSApple OSS Distributions typedef struct mnl_obj {
193*aca3beaaSApple OSS Distributions 	queue_chain_t   links;// List of mnk_name_obj (See kern/queue.h "Method 1")
194*aca3beaaSApple OSS Distributions 	mnl_name_t      name;// Unique mnl_name
195*aca3beaaSApple OSS Distributions } *mnl_obj_t;
196*aca3beaaSApple OSS Distributions 
197*aca3beaaSApple OSS Distributions #define MNL_OBJ_NULL        ((mnl_obj_t) 0UL)
198*aca3beaaSApple OSS Distributions #define MNL_OBJ_VALID(obj)  ((obj) != MNL_OBJ_NULL)
199*aca3beaaSApple OSS Distributions 
200*aca3beaaSApple OSS Distributions 
201*aca3beaaSApple OSS Distributions /*  Initialize the data structures in the mnl_obj structure at the head of the
202*aca3beaaSApple OSS Distributions  *  provided object.  This should be called on an object before it is passed to
203*aca3beaaSApple OSS Distributions  *  any other mnl_obj* routine.
204*aca3beaaSApple OSS Distributions  */
205*aca3beaaSApple OSS Distributions void mnl_obj_init(mnl_obj_t obj);
206*aca3beaaSApple OSS Distributions 
207*aca3beaaSApple OSS Distributions /*  Search the local node's hash table for the object associated with a
208*aca3beaaSApple OSS Distributions  *  mnl_name_t and return it.  Returns MNL_NAME_NULL on failure.
209*aca3beaaSApple OSS Distributions  */
210*aca3beaaSApple OSS Distributions mnl_obj_t mnl_obj_lookup(mnl_name_t name);
211*aca3beaaSApple OSS Distributions 
212*aca3beaaSApple OSS Distributions /*  Search the local node's hash table for the object associated with a
213*aca3beaaSApple OSS Distributions  *  mnl_name_t and remove it.  The pointer to the removed object is returned so
214*aca3beaaSApple OSS Distributions  *  that the caller can appropriately dispose of the object.
215*aca3beaaSApple OSS Distributions  *  Returns MNL_NAME_NULL on failure.
216*aca3beaaSApple OSS Distributions  */
217*aca3beaaSApple OSS Distributions mnl_obj_t mnl_obj_remove(mnl_name_t name);
218*aca3beaaSApple OSS Distributions 
219*aca3beaaSApple OSS Distributions /*  Insert an object into the locak node's hash table.  If the name of the
220*aca3beaaSApple OSS Distributions  *  provided object is MNL_NAME_NULL then a new mnl_name is allocated and
221*aca3beaaSApple OSS Distributions  *  assigned to the object.  Returns KERN_SUCCESS, or KERN_NAME_EXISTS if
222*aca3beaaSApple OSS Distributions  *  an object associated with that name is already in the hash table.
223*aca3beaaSApple OSS Distributions  */
224*aca3beaaSApple OSS Distributions kern_return_t mnl_obj_insert(mnl_obj_t obj);
225*aca3beaaSApple OSS Distributions 
226*aca3beaaSApple OSS Distributions 
227*aca3beaaSApple OSS Distributions /*** Mach Node Link Message Section ***
228*aca3beaaSApple OSS Distributions  *
229*aca3beaaSApple OSS Distributions  *  Struct mnl_msg is only the header for a mnl_msg buffer;
230*aca3beaaSApple OSS Distributions  *  the actual buffer is normally larger.  The rest of the buffer
231*aca3beaaSApple OSS Distributions  *  holds the body of the message to be transmitted over the link.
232*aca3beaaSApple OSS Distributions  *
233*aca3beaaSApple OSS Distributions  *  Note: A mnl_msg received over a link will be in the byte-order of the
234*aca3beaaSApple OSS Distributions  *  node that send it.  fname and size must be corrected to the hosts' native
235*aca3beaaSApple OSS Distributions  *  byte order by the link driver before it is sent up to the flipc layer.
236*aca3beaaSApple OSS Distributions  *  However, the link driver should not attempt to adjust the data model or
237*aca3beaaSApple OSS Distributions  *  byte order of the payload that follows the mnl_msg header - that will
238*aca3beaaSApple OSS Distributions  *  be done by the flipc layer.
239*aca3beaaSApple OSS Distributions  */
240*aca3beaaSApple OSS Distributions 
241*aca3beaaSApple OSS Distributions 
242*aca3beaaSApple OSS Distributions /* Values for mnl_msg.sub
243*aca3beaaSApple OSS Distributions  */
244*aca3beaaSApple OSS Distributions #define MACH_NODE_SUB_INVALID   (0) // Never sent
245*aca3beaaSApple OSS Distributions #define MACH_NODE_SUB_NODE      (1) // MNL msg is for node management
246*aca3beaaSApple OSS Distributions #define MACH_NODE_SUB_FLIPC     (2) // MNL msg is for FLIPC subsystem
247*aca3beaaSApple OSS Distributions #define MACH_NODE_SUB_VMSYS     (3) // MNL msg is for VM subsystem
248*aca3beaaSApple OSS Distributions 
249*aca3beaaSApple OSS Distributions 
250*aca3beaaSApple OSS Distributions /* Called whenever the node special port changes
251*aca3beaaSApple OSS Distributions  */
252*aca3beaaSApple OSS Distributions void mach_node_port_changed(void);
253*aca3beaaSApple OSS Distributions 
254*aca3beaaSApple OSS Distributions 
255*aca3beaaSApple OSS Distributions __END_DECLS
256*aca3beaaSApple OSS Distributions 
257*aca3beaaSApple OSS Distributions #endif  // MACH_FLIPC && MACH_KERNEL_PRIVATE
258*aca3beaaSApple OSS Distributions #endif  // _KERN_MACH_NODE_H_
259