2 .\" Copyright (c) 2006, Sun Microsystems, Inc. All Rights Reserved
3 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License.
4 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing. See the License for the specific language governing permissions and limitations under the License.
5 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
6 .TH STRUCT_DECL 9F "May 20, 2006"
8 STRUCT_DECL, SIZEOF_PTR, SIZEOF_STRUCT, STRUCT_BUF, STRUCT_FADDR, STRUCT_FGET,
9 STRUCT_FGETP, STRUCT_FSET, STRUCT_FSETP, STRUCT_HANDLE, STRUCT_INIT,
10 STRUCT_SIZE, STRUCT_SET_HANDLE \- 32-bit application data access macros
15 #include <sys/sunddi.h>
19 \fB\fR\fBSTRUCT_DECL\fR(\fB\fR\fIstructname\fR, \fB\fR \fIhandle\fR);
24 \fB\fR\fBSTRUCT_HANDLE\fR(\fB\fR\fIstructname\fR, \fB\fR\fIhandle\fR);
29 \fBvoid\fR \fBSTRUCT_INIT\fR(\fB\fR\fIhandle\fR, \fBmodel_t\fR \fIumodel\fR);
34 \fBvoid\fR \fBSTRUCT_SET_HANDLE\fR(\fB\fR\fIhandle\fR, \fBmodel_t\fR \fIumodel\fR, \fBvoid\fR \fI*addr\fR);
39 \fB\fR\fBSTRUCT_FGET\fR(\fB\fR\fIhandle\fR, \fB\fR\fIfield\fR);
44 \fB\fR\fBSTRUCT_FGETP\fR(\fB\fR\fIhandle\fR, \fB\fR\fIfield\fR);
49 \fB\fR\fBSTRUCT_FSET\fR(\fB\fR\fIhandle\fR, \fB\fR\fIfield\fR, \fB\fR\fIval\fR);
54 \fB\fR\fBSTRUCT_FSETP\fR(\fB\fR\fIhandle\fR, \fB\fR\fIfield\fR, \fB\fR\fIval\fR);
59 \fB<typeof field> *\fR\fBSTRUCT_FADDR\fR(\fB\fR\fIhandle\fR, \fB\fR\fIfield\fR);
64 \fBstruct structname *\fR\fBSTRUCT_BUF\fR(\fB\fR\fIhandle\fR);
69 \fBsize_t\fR \fBSIZEOF_STRUCT\fR(\fB\fR\fIstructname\fR, \fB\fR\fIumodel\fR);
74 \fBsize_t\fR \fBSIZEOF_PTR\fR(\fB\fR\fIumodel\fR);
79 \fBsize_t\fR \fBSTRUCT_SIZE\fR(\fB\fR\fIhandle\fR);
85 Solaris DDI specific (Solaris DDI).
89 The macros take the following parameters:
93 \fB\fIstructname\fR\fR
96 The structure name that appears \fBafter\fR the \fBC\fR keyword struct of the
106 A bit field that contains either the \fBILP32\fR model bit
107 (\fBDATAMODEL_ILP32\fR), or the \fBLP64\fR model bit (\fBDATAMODEL_LP64\fR). In
108 an \fBioctl\fR(9E), these bits are present in the flag parameter. In a
109 \fBdevmap\fR(9E), the bits are present in the model parameter \fBmmap\fR(9E).
110 The \fBddi_mmap_get_model\fR(9F) can be called to get the data model of the
120 The variable name used to refer to a particular instance of a structure which
121 is handled by these macros.
130 The field name within the structure that can contain substructures. If the
131 structures contain substructures, unions, or arrays, the \fIfield\fR can be
132 whatever complex expression would naturally follow the first . or ->.
138 The above macros allow a device driver to access data consumed from a 32-bit
139 application regardless whether the driver was compiled to the ILP32 or LP64
140 data model. These macros effectively hide the difference between the data model
141 of the user application and the driver.
144 The macros can be broken up into two main categories described in the following
146 .SS "Declaration and Initialization Macros"
149 The macros \fBSTRUCT_DECL()\fR and \fBSTRUCT_HANDLE()\fR declare structure
150 handles on the stack, whereas the macros \fBSTRUCT_INIT()\fR and
151 \fBSTRUCT_SET_HANDLE()\fR initialize the structure handles to point to an
152 instance of the native form structure.
155 The macros \fBSTRUCT_HANDLE()\fR and \fBSTRUCT_SET_HANDLE()\fR are used to
156 declare and initialize a structure handle to an existing data structure, for
157 example, ioctls within a STREAMS module.
160 The macros \fBSTRUCT_DECL()\fR and \fBSTRUCT_INIT()\fR, on the other hand, are
161 used in modules which declare and initialize a structure handle to a data
162 structure allocated by \fBSTRUCT_DECL()\fR, that is, any standard character or
163 block device driver \fBioctl\fR(9E) routine that needs to copy in data from a
168 \fBSTRUCT_DECL(structname, handle)\fR
172 Declares a structure handle for a struct and allocates an instance of its
173 native form on the stack. It is assumed that the native form is larger than or
174 equal to the ILP32 form. \fIhandle\fR is a variable name and is declared as a
175 variable by this macro.
181 \fBvoid STRUCT_INIT(handle, model_t umodel)\fR
185 Initializes \fIhandle\fR to point to the instance allocated by
186 \fBSTRUCT_DECL()\fR. It also sets data model for \fIhandle\fR to \fIumodel\fR
187 and it must be called before any access is made through the macros that operate
188 on these structures. When used in an \fBioctl\fR(9E) routine, \fIumodel\fR is
189 the flag parameter. In a \fBdevmap\fR(9E) routine, \fIumodel\fR is the model
190 parameter. In a \fBmmap\fR(9E) routine, \fIumodel\fR is the return value of
191 \fBddi_mmap_get_model\fR(9F). This macro is intended only for handles created
192 with \fBSTRUCT_DECL()\fR.
198 \fBSTRUCT_HANDLE(structname, handle)\fR
202 Declares a structure handle \fIhandle\fR but, unlike \fBSTRUCT_DECL()\fR, it
203 does not allocate an instance of "struct".
209 \fBvoid STRUCT_SET_HANDLE(handle, model_t umodel, void *addr)\fR
213 Initializes handle to point to the native form instance at \fIaddr\fR. It also
214 sets the data model for \fIhandle\fR to \fIumodel\fR. This is intended for
215 handles created with \fBSTRUCT_HANDLE()\fR. Fields cannot be referenced via the
216 \fIhandle\fR until this macro has been invoked. Typically, \fIaddr\fR is the
217 address of the native form structure containing the user-mode programs data.
218 When used in an \fBioctl\fR(9E), \fIumodel\fR is the flag parameter. In a
219 \fBdevmap\fR(9E) routine, \fIumodel\fR is the model parameter. In an
220 \fBmmap\fR(9E) routine, \fIumodel\fR is the return value of
221 \fBddi_mmap_get_model\fR(9F).
224 .SS "Operation Macros"
228 \fBsize_t STRUCT_SIZE(handle)\fR
232 Returns size of the structure referred to by \fIhandle\fR, depending on the
233 data model associated with \fIhandle\fR. If the data model stored by
234 \fBSTRUCT_INIT()\fR or \fBSTRUCT_SET_HANDLE()\fR is \fBDATAMODEL_ILP32\fR, the
235 size of the \fBILP32\fR form is returned. Otherwise, the size of the native
242 \fBSTRUCT_FGET(handle, field)\fR
246 Returns the contents of \fIfield\fR in the structure described by \fIhandle\fR
247 according to the data model associated with \fIhandle\fR.
253 \fBSTRUCT_FGETP(handle, field)\fR
257 This is the same as \fBSTRUCT_FGET()\fR except that the \fIfield\fR in question
258 is a pointer of some kind. This macro casts caddr32_t to a (void *) when it is
259 accessed. Failure to use this macro for a pointer leads to compiler warnings or
266 \fBSTRUCT_FSET(handle, field, val)\fR
270 Assigns \fIval\fR to the (non-pointer) in the structure described by
271 \fIhandle\fR. It should not be used within another expression, but only as a
278 \fBSTRUCT_FSETP(handle, field, val)\fR
282 This is the equivalent of STRUCT_FGETP() for STRUCT_FGET(), with the same
283 exceptions. Like STRUCT_FSET, STRUCT_FSETP should not be used within another
284 expression, but only as a statement.
290 \fBstruct structname *STRUCT_BUF(handle)\fR
294 Returns a pointer to the native mode instance of the structure described by
298 .SS "Macros Not Using Handles"
302 \fBsize_t SIZEOF_STRUCT(structname, umodel)\fR
306 Returns size of \fIstructname\fR based on \fIumodel\fR.
312 \fBsize_t SIZEOF_PTR(umodel)\fR
316 Returns the size of a pointer based on \fIumodel\fR.
321 \fBExample 1 \fRCopying a Structure
324 The following example uses an \fBioctl\fR(9E) on a regular character device
325 that copies a data structure that looks like this into the kernel:
338 \fBExample 2 \fRDefining a Structure
341 This data structure definition describes what the \fBioctl\fR(9E) would look
342 like in a 32-bit application using fixed width types.
347 #if defined(_MULTI_DATAMODEL)
357 \fBExample 3 \fRUsing \fBSTRUCT_DECL()\fR and \fBSTRUCT_INIT()\fR
360 Note: This example uses the \fBSTRUCT_DECL()\fR and \fBSTRUCT_INIT()\fR macros
361 to declare and initialize the structure handle.
367 xxioctl(dev_t dev, int cmd, intptr_t arg, int mode,
368 cred_t *cr, int *rval_p);
370 STRUCT_DECL(opdata, op);
375 STRUCT_INIT(op, mode);
377 if (copyin((void *)data,
378 STRUCT_BUF(op), STRUCT_SIZE(op)))
381 if (STRUCT_FGET(op, flag) != FACTIVE ||
382 STRUCT_FGET(op, size) > sizeof (device_state))
384 xxdowork(device_state, STRUCT_FGET(op, size));
392 This piece of code is an excerpt from a STREAMS module that handles
393 \fBioctl\fR(9E) data (M_IOCDATA) messages and uses the data structure defined
394 above. This code has been written to run in the ILP32 environment only.
397 \fBExample 4 \fRUsing \fBSTRUCT_HANDLE()\fR and \fBSTRUCT_SET_HANDLE()\fR
400 The next example illustrates the use of the \fBSTRUCT_HANDLE()\fR and
401 \fBSTRUCT_SET_HANDLE()\fR macros which declare and initialize the structure
402 handle to point to an already existing instance of the structure.
406 The above code example can be converted to run in the LP64 environment using
407 the \fBSTRUCT_HANDLE()\fR and \fBSTRUCT_SET_HANDLE()\fR as follows:
413 int maxlen; /* no. of bytes in buffer */
414 int len; /* no. of bytes returned */
415 caddr_t buf; /* pointer to data */
424 wput_iocdata(queue_t *q, mblk_t *msgp)
426 struct copyresp *cp = (struct copyresp *)msgp->b_rptr;
427 STRUCT_HANDLE(strbuf, sb);
429 if (msgp->b_cont->b_cont != NULL) {
430 msgp->b_cont = msgpullup(msgp->b_cont, -1);
431 if (msgp->b_cont == NULL) {
432 miocnak(q, msgp, 0, ENOSR);
436 STRUCT_SET_HANDLE(sb, cp->cp_flag, (void *)msgp->b_cont->b_rptr);
437 if (STRUCT_FGET(sb, maxlen) < (int)sizeof (ipa_t)) {
438 miocnak(q, msgp, 0, ENOSR);
442 miocack(q, msgp, 0, 0);
450 See \fBattributes\fR(5) for descriptions of the following attributes:
458 ATTRIBUTE TYPE ATTRIBUTE VALUE
460 Interface Stability Evolving
466 \fBdevmap\fR(9E), \fBioctl\fR(9E), \fBmmap\fR(9E), \fBddi_mmap_get_model\fR(9F)
469 \fIWriting Device Drivers\fR
472 \fISTREAMS Programming Guide\fR