xref: /minix3/minix/kernel/system/do_safememset.c (revision 9624407e7addfd8b88486acfe3a0e056e2b92ee3)
1*433d6423SLionel Sambuc /* The kernel call implemented in this file:
2*433d6423SLionel Sambuc  *   m_type:	SYS_SAFEMEMSET
3*433d6423SLionel Sambuc  *
4*433d6423SLionel Sambuc  * The parameters for this kernel call are:
5*433d6423SLionel Sambuc  *	SMS_DST		dst endpoint
6*433d6423SLionel Sambuc  *	SMS_GID		grant id
7*433d6423SLionel Sambuc  *	SMS_OFFSET	offset within grant
8*433d6423SLionel Sambuc  *	SMS_PATTERN     memset pattern byte
9*433d6423SLionel Sambuc  *	SMS_BYTES	bytes from offset
10*433d6423SLionel Sambuc  */
11*433d6423SLionel Sambuc #include <assert.h>
12*433d6423SLionel Sambuc 
13*433d6423SLionel Sambuc #include <minix/safecopies.h>
14*433d6423SLionel Sambuc 
15*433d6423SLionel Sambuc #include "kernel/system.h"
16*433d6423SLionel Sambuc 
17*433d6423SLionel Sambuc /*===========================================================================*
18*433d6423SLionel Sambuc  *                              do_safememset                                *
19*433d6423SLionel Sambuc  *===========================================================================*/
do_safememset(struct proc * caller,message * m_ptr)20*433d6423SLionel Sambuc int do_safememset(struct proc *caller, message *m_ptr) {
21*433d6423SLionel Sambuc 	/* Implementation of the do_safememset() kernel call */
22*433d6423SLionel Sambuc 
23*433d6423SLionel Sambuc 	/* Extract parameters */
24*433d6423SLionel Sambuc 	endpoint_t dst_endpt = m_ptr->SMS_DST;
25*433d6423SLionel Sambuc 	endpoint_t caller_endpt = caller->p_endpoint;
26*433d6423SLionel Sambuc 	cp_grant_id_t grantid = m_ptr->SMS_GID;
27*433d6423SLionel Sambuc 	vir_bytes g_offset = m_ptr->SMS_OFFSET;
28*433d6423SLionel Sambuc 	int pattern = m_ptr->SMS_PATTERN;
29*433d6423SLionel Sambuc 	size_t len = (size_t)m_ptr->SMS_BYTES;
30*433d6423SLionel Sambuc 
31*433d6423SLionel Sambuc 	struct proc *dst_p;
32*433d6423SLionel Sambuc 	endpoint_t new_granter;
33*433d6423SLionel Sambuc 	static vir_bytes v_offset;
34*433d6423SLionel Sambuc 	int r;
35*433d6423SLionel Sambuc 
36*433d6423SLionel Sambuc 	if (dst_endpt == NONE || caller_endpt == NONE)
37*433d6423SLionel Sambuc 		return EFAULT;
38*433d6423SLionel Sambuc 
39*433d6423SLionel Sambuc 	if (!(dst_p = endpoint_lookup(dst_endpt)))
40*433d6423SLionel Sambuc 		return EINVAL;
41*433d6423SLionel Sambuc 
42*433d6423SLionel Sambuc 	if (!(priv(dst_p) && priv(dst_p)->s_grant_table)) {
43*433d6423SLionel Sambuc 		printf("safememset: dst %d has no grant table\n", dst_endpt);
44*433d6423SLionel Sambuc 		return EINVAL;
45*433d6423SLionel Sambuc 	}
46*433d6423SLionel Sambuc 
47*433d6423SLionel Sambuc 	/* Verify permission exists, memset always requires CPF_WRITE */
48*433d6423SLionel Sambuc 	r = verify_grant(dst_endpt, caller_endpt, grantid, len, CPF_WRITE,
49*433d6423SLionel Sambuc 			 g_offset, &v_offset, &new_granter, NULL);
50*433d6423SLionel Sambuc 
51*433d6423SLionel Sambuc 	if (r != OK) {
52*433d6423SLionel Sambuc 		printf("safememset: grant %d verify failed %d", grantid, r);
53*433d6423SLionel Sambuc 		return r;
54*433d6423SLionel Sambuc 	}
55*433d6423SLionel Sambuc 
56*433d6423SLionel Sambuc 	return vm_memset(caller, new_granter, v_offset, pattern, len);
57*433d6423SLionel Sambuc }
58