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