kern_physio.c revision 1887
1/*
2 * Copyright (c) 1994 John S. Dyson
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice immediately at the beginning of the file, without modification,
10 *    this list of conditions, and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. Absolutely no warranty of function or purpose is made by the author
15 *    John S. Dyson.
16 * 4. Modifications may be freely made to this file if the above conditions
17 *    are met.
18 *
19 * $Id: kern_physio.c,v 1.3 1994/08/02 07:42:05 davidg Exp $
20 */
21
22#include <sys/param.h>
23#include <sys/systm.h>
24#include <sys/buf.h>
25#include <sys/conf.h>
26#include <sys/proc.h>
27#include <vm/vm.h>
28
29static void physwakeup();
30
31int
32physio(strategy, bp, dev, rw, minp, uio)
33	int (*strategy)();
34	struct buf *bp;
35	dev_t dev;
36	int rw;
37	u_int (*minp)();
38	struct uio *uio;
39{
40	int i;
41	int bufflags = rw?B_READ:0;
42	int error;
43	int spl;
44	caddr_t sa;
45	int bp_alloc = (bp == 0);
46	struct buf *bpa;
47
48/*
49 * keep the process from being swapped
50 */
51	curproc->p_flag |= P_PHYSIO;
52
53	/* create and build a buffer header for a transfer */
54	bpa = (struct buf *)getpbuf();
55	if (!bp_alloc) {
56		spl = splbio();
57		while (bp->b_flags & B_BUSY) {
58			bp->b_flags |= B_WANTED;
59			tsleep((caddr_t)bp, PRIBIO, "physbw", 0);
60		}
61		bp->b_flags |= B_BUSY;
62		splx(spl);
63	} else {
64		bp = bpa;
65	}
66
67	/*
68	 * get a copy of the kva from the physical buffer
69	 */
70	sa = bpa->b_data;
71	bp->b_proc = curproc;
72	bp->b_dev = dev;
73	error = bp->b_error = 0;
74
75	for(i=0;i<uio->uio_iovcnt;i++) {
76		while( uio->uio_iov[i].iov_len) {
77			vm_offset_t v, lastv, pa;
78			caddr_t adr;
79
80			bp->b_bcount = uio->uio_iov[i].iov_len;
81			bp->b_bufsize = bp->b_bcount;
82			bp->b_flags = B_BUSY | B_PHYS | B_CALL | bufflags;
83			bp->b_iodone = physwakeup;
84			bp->b_data = uio->uio_iov[i].iov_base;
85			/*
86			 * pass in the kva from the physical buffer
87			 * for the temporary kernel mapping.
88			 */
89			bp->b_saveaddr = sa;
90			bp->b_blkno = btodb(uio->uio_offset);
91
92
93			if (rw && !useracc(bp->b_data, bp->b_bufsize, B_WRITE)) {
94				error = EFAULT;
95				goto doerror;
96			}
97			if (!rw && !useracc(bp->b_data, bp->b_bufsize, B_READ)) {
98				error = EFAULT;
99				goto doerror;
100			}
101
102			vmapbuf(bp);
103
104			/* perform transfer */
105			(*strategy)(bp);
106
107			spl = splbio();
108			while ((bp->b_flags & B_DONE) == 0)
109				tsleep((caddr_t)bp, PRIBIO, "physstr", 0);
110			splx(spl);
111
112			vunmapbuf(bp);
113
114			/*
115			 * update the uio data
116			 */
117			{
118				int iolen = bp->b_bcount - bp->b_resid;
119				uio->uio_iov[i].iov_len -= iolen;
120				uio->uio_iov[i].iov_base += iolen;
121				uio->uio_resid -= iolen;
122				uio->uio_offset += iolen;
123			}
124
125			/*
126			 * check for an error
127			 */
128			if( bp->b_flags & B_ERROR) {
129				error = bp->b_error;
130				goto doerror;
131			}
132		}
133	}
134
135
136doerror:
137	relpbuf(bpa);
138	if (!bp_alloc) {
139		bp->b_flags &= ~(B_BUSY|B_PHYS);
140		if( bp->b_flags & B_WANTED) {
141			bp->b_flags &= ~B_WANTED;
142			wakeup((caddr_t)bp);
143		}
144	}
145/*
146 * allow the process to be swapped
147 */
148	curproc->p_flag &= ~P_PHYSIO;
149
150	return (error);
151}
152
153u_int
154minphys(struct buf *bp)
155{
156
157	if( bp->b_bcount > MAXBSIZE) {
158		bp->b_bcount = MAXBSIZE;
159	}
160	return bp->b_bcount;
161}
162
163int
164rawread(dev_t dev, struct uio *uio)
165{
166	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
167	    dev, 1, minphys, uio));
168}
169
170int
171rawwrite(dev_t dev, struct uio *uio)
172{
173	return (physio(cdevsw[major(dev)].d_strategy, (struct buf *)NULL,
174	    dev, 0, minphys, uio));
175}
176
177static void
178physwakeup(bp)
179	struct buf *bp;
180{
181	wakeup((caddr_t) bp);
182	bp->b_flags &= ~B_CALL;
183}
184