xref: /minix/minix/kernel/arch/i386/do_sdevio.c (revision 9e6b1315)
1433d6423SLionel Sambuc /* The kernel call implemented in this file:
2433d6423SLionel Sambuc  *   m_type:	SYS_SDEVIO
3433d6423SLionel Sambuc  *
4433d6423SLionel Sambuc  * The parameters for this kernel call are:
5433d6423SLionel Sambuc  *    m_lsys_krn_sys_sdevio.request	(request input or output)
6433d6423SLionel Sambuc  *    m_lsys_krn_sys_sdevio.port	(port to read/ write)
7433d6423SLionel Sambuc  *    m_lsys_krn_sys_sdevio.vec_addr	(virtual address of buffer, or grant ID)
8433d6423SLionel Sambuc  *    m_lsys_krn_sys_sdevio.vec_size	(number of elements)
9433d6423SLionel Sambuc  *    m_lsys_krn_sys_sdevio.vec_endpt	(process where buffer is)
10433d6423SLionel Sambuc  *    m_lsys_krn_sys_sdevio.offset	(offset into the grant)
11433d6423SLionel Sambuc  */
12433d6423SLionel Sambuc 
13433d6423SLionel Sambuc #include "kernel/system.h"
14433d6423SLionel Sambuc #include <minix/devio.h>
15433d6423SLionel Sambuc #include <minix/endpoint.h>
16433d6423SLionel Sambuc 
17433d6423SLionel Sambuc #include "arch_proto.h"
18433d6423SLionel Sambuc 
19433d6423SLionel Sambuc #if USE_SDEVIO
20433d6423SLionel Sambuc 
21433d6423SLionel Sambuc /*===========================================================================*
22433d6423SLionel Sambuc  *			        do_sdevio                                    *
23433d6423SLionel Sambuc  *===========================================================================*/
do_sdevio(struct proc * caller,message * m_ptr)24433d6423SLionel Sambuc int do_sdevio(struct proc * caller, message *m_ptr)
25433d6423SLionel Sambuc {
26433d6423SLionel Sambuc   vir_bytes newoffset;
27433d6423SLionel Sambuc   endpoint_t newep;
28433d6423SLionel Sambuc   int proc_nr;
29433d6423SLionel Sambuc   endpoint_t proc_nr_e = m_ptr->m_lsys_krn_sys_sdevio.vec_endpt;
30433d6423SLionel Sambuc   vir_bytes count = m_ptr->m_lsys_krn_sys_sdevio.vec_size;
31433d6423SLionel Sambuc   long port = m_ptr->m_lsys_krn_sys_sdevio.port;
32433d6423SLionel Sambuc   phys_bytes vir_buf;
33*9e6b1315SCristiano Giuffrida   int i, r, req_type, req_dir, size, nr_io_range;
34433d6423SLionel Sambuc   struct priv *privp;
35433d6423SLionel Sambuc   struct io_range *iorp;
36433d6423SLionel Sambuc   struct proc *destproc;
37433d6423SLionel Sambuc   int retval;
38433d6423SLionel Sambuc 
39433d6423SLionel Sambuc   /* Allow safe copies and accesses to SELF */
40433d6423SLionel Sambuc   if ((m_ptr->m_lsys_krn_sys_sdevio.request & _DIO_SAFEMASK) != _DIO_SAFE &&
41433d6423SLionel Sambuc 	proc_nr_e != SELF)
42433d6423SLionel Sambuc   {
43433d6423SLionel Sambuc 	static int first= 1;
44433d6423SLionel Sambuc 	if (first)
45433d6423SLionel Sambuc 	{
46433d6423SLionel Sambuc 		first= 0;
47433d6423SLionel Sambuc 		printf("do_sdevio: for %d, req %d\n",
48433d6423SLionel Sambuc 			m_ptr->m_source, m_ptr->m_lsys_krn_sys_sdevio.request);
49433d6423SLionel Sambuc 	}
50433d6423SLionel Sambuc   }
51433d6423SLionel Sambuc 
52433d6423SLionel Sambuc   /* Check if process endpoint is OK.
53433d6423SLionel Sambuc    * A driver may directly provide a pointer to a buffer at the user-process
54433d6423SLionel Sambuc    * that initiated the device I/O. Kernel processes, of course, are denied.
55433d6423SLionel Sambuc    */
56433d6423SLionel Sambuc   if (proc_nr_e == SELF)
57433d6423SLionel Sambuc 	okendpt(caller->p_endpoint, &proc_nr);
58433d6423SLionel Sambuc   else
59433d6423SLionel Sambuc 	if(!isokendpt(proc_nr_e, &proc_nr))
60433d6423SLionel Sambuc 		return(EINVAL);
61433d6423SLionel Sambuc   if (iskerneln(proc_nr)) return(EPERM);
62433d6423SLionel Sambuc 
63433d6423SLionel Sambuc   /* Extract direction (in or out) and type (size). */
64433d6423SLionel Sambuc   req_dir = m_ptr->m_lsys_krn_sys_sdevio.request & _DIO_DIRMASK;
65433d6423SLionel Sambuc   req_type = m_ptr->m_lsys_krn_sys_sdevio.request & _DIO_TYPEMASK;
66433d6423SLionel Sambuc 
67433d6423SLionel Sambuc   /* Check for 'safe' variants. */
68433d6423SLionel Sambuc   if((m_ptr->m_lsys_krn_sys_sdevio.request & _DIO_SAFEMASK) == _DIO_SAFE) {
69433d6423SLionel Sambuc      /* Map grant address to physical address. */
70*9e6b1315SCristiano Giuffrida      if((r=verify_grant(proc_nr_e, caller->p_endpoint,
71433d6423SLionel Sambuc 		m_ptr->m_lsys_krn_sys_sdevio.vec_addr, count,
72433d6423SLionel Sambuc 		req_dir == _DIO_INPUT ? CPF_WRITE : CPF_READ,
73433d6423SLionel Sambuc 		m_ptr->m_lsys_krn_sys_sdevio.offset, &newoffset, &newep,
74*9e6b1315SCristiano Giuffrida 		NULL)) != OK) {
75*9e6b1315SCristiano Giuffrida 	if(r == ENOTREADY) return r;
76433d6423SLionel Sambuc 	printf("do_sdevio: verify_grant failed\n");
77433d6423SLionel Sambuc 	return EPERM;
78433d6423SLionel Sambuc     }
79433d6423SLionel Sambuc 	if(!isokendpt(newep, &proc_nr))
80433d6423SLionel Sambuc 		return(EINVAL);
81433d6423SLionel Sambuc      destproc = proc_addr(proc_nr);
82433d6423SLionel Sambuc      vir_buf = newoffset;
83433d6423SLionel Sambuc   } else {
84433d6423SLionel Sambuc      if(proc_nr != _ENDPOINT_P(caller->p_endpoint))
85433d6423SLionel Sambuc      {
86433d6423SLionel Sambuc 	printf("do_sdevio: unsafe sdevio by %d in %d denied\n",
87433d6423SLionel Sambuc 		caller->p_endpoint, proc_nr_e);
88433d6423SLionel Sambuc 	return EPERM;
89433d6423SLionel Sambuc      }
90433d6423SLionel Sambuc      /* Get and check physical address. */
91433d6423SLionel Sambuc      vir_buf = m_ptr->m_lsys_krn_sys_sdevio.vec_addr;
92433d6423SLionel Sambuc      destproc = proc_addr(proc_nr);
93433d6423SLionel Sambuc   }
94433d6423SLionel Sambuc      /* current process must be target for phys_* to be OK */
95433d6423SLionel Sambuc 
96433d6423SLionel Sambuc   switch_address_space(destproc);
97433d6423SLionel Sambuc 
98433d6423SLionel Sambuc   switch (req_type)
99433d6423SLionel Sambuc   {
100433d6423SLionel Sambuc 	case _DIO_BYTE: size= 1; break;
101433d6423SLionel Sambuc 	case _DIO_WORD: size= 2; break;
102433d6423SLionel Sambuc 	case _DIO_LONG: size= 4; break;
103433d6423SLionel Sambuc 	default: size= 4; break;	/* Be conservative */
104433d6423SLionel Sambuc   }
105433d6423SLionel Sambuc 
106433d6423SLionel Sambuc   privp= priv(caller);
107433d6423SLionel Sambuc   if (privp && privp->s_flags & CHECK_IO_PORT)
108433d6423SLionel Sambuc   {
109433d6423SLionel Sambuc 	port= m_ptr->m_lsys_krn_sys_sdevio.port;
110433d6423SLionel Sambuc 	nr_io_range= privp->s_nr_io_range;
111433d6423SLionel Sambuc 	for (i= 0, iorp= privp->s_io_tab; i<nr_io_range; i++, iorp++)
112433d6423SLionel Sambuc 	{
113433d6423SLionel Sambuc 		if (port >= iorp->ior_base && port+size-1 <= iorp->ior_limit)
114433d6423SLionel Sambuc 			break;
115433d6423SLionel Sambuc 	}
116433d6423SLionel Sambuc 	if (i >= nr_io_range)
117433d6423SLionel Sambuc 	{
118433d6423SLionel Sambuc 		printf(
119433d6423SLionel Sambuc 		"do_sdevio: I/O port check failed for proc %d, port 0x%x\n",
120433d6423SLionel Sambuc 			m_ptr->m_source, port);
121433d6423SLionel Sambuc 		retval = EPERM;
122433d6423SLionel Sambuc 		goto return_error;
123433d6423SLionel Sambuc 	}
124433d6423SLionel Sambuc   }
125433d6423SLionel Sambuc 
126433d6423SLionel Sambuc   if (port & (size-1))
127433d6423SLionel Sambuc   {
128433d6423SLionel Sambuc 	printf("do_devio: unaligned port 0x%x (size %d)\n", port, size);
129433d6423SLionel Sambuc 	retval = EPERM;
130433d6423SLionel Sambuc 	goto return_error;
131433d6423SLionel Sambuc   }
132433d6423SLionel Sambuc 
133433d6423SLionel Sambuc   /* Perform device I/O for bytes and words. Longs are not supported. */
134433d6423SLionel Sambuc   if (req_dir == _DIO_INPUT) {
135433d6423SLionel Sambuc       switch (req_type) {
136433d6423SLionel Sambuc       case _DIO_BYTE: phys_insb(port, vir_buf, count); break;
137433d6423SLionel Sambuc       case _DIO_WORD: phys_insw(port, vir_buf, count); break;
138433d6423SLionel Sambuc       default:
139433d6423SLionel Sambuc   		retval = EINVAL;
140433d6423SLionel Sambuc 		goto return_error;
141433d6423SLionel Sambuc       }
142433d6423SLionel Sambuc   } else if (req_dir == _DIO_OUTPUT) {
143433d6423SLionel Sambuc       switch (req_type) {
144433d6423SLionel Sambuc       case _DIO_BYTE: phys_outsb(port, vir_buf, count); break;
145433d6423SLionel Sambuc       case _DIO_WORD: phys_outsw(port, vir_buf, count); break;
146433d6423SLionel Sambuc       default:
147433d6423SLionel Sambuc   		retval = EINVAL;
148433d6423SLionel Sambuc 		goto return_error;
149433d6423SLionel Sambuc       }
150433d6423SLionel Sambuc   }
151433d6423SLionel Sambuc   else {
152433d6423SLionel Sambuc 	  retval = EINVAL;
153433d6423SLionel Sambuc 	  goto return_error;
154433d6423SLionel Sambuc   }
155433d6423SLionel Sambuc   retval = OK;
156433d6423SLionel Sambuc 
157433d6423SLionel Sambuc return_error:
158433d6423SLionel Sambuc   /* switch back to the address of the process which made the call */
159433d6423SLionel Sambuc   switch_address_space(caller);
160433d6423SLionel Sambuc   return retval;
161433d6423SLionel Sambuc }
162433d6423SLionel Sambuc 
163433d6423SLionel Sambuc #endif /* USE_SDEVIO */
164