2 /* Library functions to maintain internal data copying tables.
4 * April 21 2006: Initial version (Ben Gras)
10 #include <minix/sysutil.h>
13 #include <minix/syslib.h>
14 #include <minix/safecopies.h>
15 #include <minix/com.h>
18 #define ACCESS_CHECK(a) { \
19 if((a) & ~(CPF_READ|CPF_WRITE|CPF_MAP)) { \
25 #define GID_CHECK(gid) { \
26 if(!GRANT_VALID(gid) || (gid) < 0 || (gid) >= ngrants) {\
32 #define GID_CHECK_USED(gid) { \
34 if(!(grants[gid].cp_flags & CPF_USED)) { \
40 #define CLICK_ALIGNMENT_CHECK(addr, bytes) { \
41 if(((vir_bytes)(addr) % CLICK_SIZE != 0) \
42 || ((vir_bytes)(bytes) % CLICK_SIZE != 0)) { \
47 #define NR_STATIC_GRANTS 2
48 static cp_grant_t static_grants
[NR_STATIC_GRANTS
];
49 static cp_grant_t
*grants
= NULL
;
50 static int ngrants
= 0;
55 /* Grow the grants table if possible. */
56 cp_grant_t
*new_grants
;
61 /* Use statically allocated grants the first time. */
62 new_size
= NR_STATIC_GRANTS
;
63 new_grants
= static_grants
;
66 new_size
= (1+ngrants
)*2;
67 assert(new_size
> ngrants
);
69 /* Allocate a block of new size. */
70 if(!(new_grants
=malloc(new_size
* sizeof(grants
[0])))) {
75 /* Copy old block to new block. */
76 if(grants
&& ngrants
> 0)
77 memcpy(new_grants
, grants
, ngrants
* sizeof(grants
[0]));
79 /* Make sure new slots are marked unused (CPF_USED is clear). */
80 for(g
= ngrants
; g
< new_size
; g
++)
81 new_grants
[g
].cp_flags
= 0;
83 /* Inform kernel about new size (and possibly new location). */
84 if((sys_setgrant(new_grants
, new_size
))) {
85 if(new_grants
!= static_grants
) free(new_grants
);
86 return; /* Failed - don't grow then. */
89 /* Update internal data. */
90 if(grants
&& ngrants
> 0 && grants
!= static_grants
) free(grants
);
96 cpf_new_grantslot(void)
98 /* Find a new, free grant slot in the grant table, grow it if
99 * necessary. If no free slot is found and the grow failed,
100 * return -1. Otherwise, return grant slot number.
104 /* Find free slot. */
105 for(g
= 0; g
< ngrants
&& (grants
[g
].cp_flags
& CPF_USED
); g
++)
108 assert(g
<= ngrants
);
110 /* No free slot found? */
113 assert(g
<= ngrants
); /* ngrants can't shrink. */
115 /* ngrants hasn't increased. */
121 /* Basic sanity checks - if we get this far, g must be a valid,
124 assert(GRANT_VALID(g
));
127 assert(!(grants
[g
].cp_flags
& CPF_USED
));
133 cpf_grant_direct(endpoint_t who_to
, vir_bytes addr
, size_t bytes
, int access
)
138 /* Get new slot to put new grant in. */
139 if((g
= cpf_new_grantslot()) < 0)
140 return(GRANT_INVALID
);
142 assert(GRANT_VALID(g
));
145 assert(!(grants
[g
].cp_flags
& CPF_USED
));
147 if((r
=cpf_setgrant_direct(g
, who_to
, addr
, bytes
, access
)) < 0) {
149 return(GRANT_INVALID
);
156 cpf_grant_indirect(endpoint_t who_to
, endpoint_t who_from
, cp_grant_id_t gr
)
158 /* Grant process A access into process B. B has granted us access as grant
164 /* Obtain new slot. */
165 if((g
= cpf_new_grantslot()) < 0)
168 /* Basic sanity checks. */
169 assert(GRANT_VALID(g
));
172 assert(!(grants
[g
].cp_flags
& CPF_USED
));
174 /* Fill in new slot data. */
175 if((r
=cpf_setgrant_indirect(g
, who_to
, who_from
, gr
)) < 0) {
177 return GRANT_INVALID
;
184 cpf_grant_magic(endpoint_t who_to
, endpoint_t who_from
,
185 vir_bytes addr
, size_t bytes
, int access
)
187 /* Grant process A access into process B. Not everyone can do this. */
191 ACCESS_CHECK(access
);
193 /* Obtain new slot. */
194 if((g
= cpf_new_grantslot()) < 0)
197 /* Basic sanity checks. */
198 assert(GRANT_VALID(g
));
201 assert(!(grants
[g
].cp_flags
& CPF_USED
));
203 if((r
=cpf_setgrant_magic(g
, who_to
, who_from
, addr
,
204 bytes
, access
)) < 0) {
213 cpf_revoke(cp_grant_id_t g
)
215 /* Revoke previously granted access, identified by grant id. */
219 /* If this grant is for memory mapping, revoke the mapping first. */
220 if(grants
[g
].cp_flags
& CPF_MAP
) {
221 r
= sys_saferevmap_gid(g
);
226 /* Make grant invalid by setting flags to 0, clearing CPF_USED.
227 * This invalidates the grant.
229 grants
[g
].cp_flags
= 0;
235 cpf_lookup(cp_grant_id_t g
, endpoint_t
*granter
, endpoint_t
*grantee
)
237 /* First check slot validity, and if it's in use currently. */
240 if(grants
[g
].cp_flags
& CPF_DIRECT
) {
241 if(granter
) *granter
= SELF
;
242 if(grantee
) *grantee
= grants
[g
].cp_u
.cp_direct
.cp_who_to
;
243 } else if(grants
[g
].cp_flags
& CPF_MAGIC
) {
244 if(granter
) *granter
= grants
[g
].cp_u
.cp_magic
.cp_who_from
;
245 if(grantee
) *grantee
= grants
[g
].cp_u
.cp_magic
.cp_who_to
;
252 cpf_getgrants(cp_grant_id_t
*grant_ids
, int n
)
256 for(i
= 0; i
< n
; i
++) {
257 if((grant_ids
[i
] = cpf_new_grantslot()) < 0)
259 grants
[grant_ids
[i
]].cp_flags
= CPF_USED
;
262 /* return however many grants were assigned. */
267 cpf_setgrant_direct(gid
, who
, addr
, bytes
, access
)
275 ACCESS_CHECK(access
);
277 /* Check click alignment in case of memory mapping grant. */
278 if(access
& CPF_MAP
) {
279 CLICK_ALIGNMENT_CHECK(addr
, bytes
);
282 /* Fill in new slot data. */
283 grants
[gid
].cp_flags
= access
| CPF_DIRECT
| CPF_USED
| CPF_VALID
;
284 grants
[gid
].cp_u
.cp_direct
.cp_who_to
= who
;
285 grants
[gid
].cp_u
.cp_direct
.cp_start
= addr
;
286 grants
[gid
].cp_u
.cp_direct
.cp_len
= bytes
;
292 cpf_setgrant_indirect(gid
, who_to
, who_from
, his_gid
)
294 endpoint_t who_to
, who_from
;
295 cp_grant_id_t his_gid
;
299 /* Fill in new slot data. */
300 grants
[gid
].cp_flags
= CPF_USED
| CPF_INDIRECT
| CPF_VALID
;
301 grants
[gid
].cp_u
.cp_indirect
.cp_who_to
= who_to
;
302 grants
[gid
].cp_u
.cp_indirect
.cp_who_from
= who_from
;
303 grants
[gid
].cp_u
.cp_indirect
.cp_grant
= his_gid
;
309 cpf_setgrant_magic(gid
, who_to
, who_from
, addr
, bytes
, access
)
311 endpoint_t who_to
, who_from
;
317 ACCESS_CHECK(access
);
319 /* Check click alignment in case of memory mapping grant. */
320 if(access
& CPF_MAP
) {
321 CLICK_ALIGNMENT_CHECK(addr
, bytes
);
324 /* Fill in new slot data. */
325 grants
[gid
].cp_flags
= CPF_USED
| CPF_MAGIC
| CPF_VALID
| access
;
326 grants
[gid
].cp_u
.cp_magic
.cp_who_to
= who_to
;
327 grants
[gid
].cp_u
.cp_magic
.cp_who_from
= who_from
;
328 grants
[gid
].cp_u
.cp_magic
.cp_start
= addr
;
329 grants
[gid
].cp_u
.cp_magic
.cp_len
= bytes
;
335 cpf_setgrant_disable(gid
)
340 /* Grant is now no longer valid, but still in use. */
341 grants
[gid
].cp_flags
= CPF_USED
;
349 /* Inform the kernel about the location of the grant table. This is needed
353 sys_setgrant(grants
, ngrants
); /* Do we need error checking? */