sun merge
[unix-history] / usr / src / sys / kern / kern_physio.c
CommitLineData
961945a8
SL
1/* kern_physio.c 4.37 82/12/17 */
2
3#include "../machine/pte.h"
663dbc72
BJ
4
5#include "../h/param.h"
6#include "../h/systm.h"
7#include "../h/dir.h"
8#include "../h/user.h"
9#include "../h/buf.h"
10#include "../h/conf.h"
11#include "../h/proc.h"
12#include "../h/seg.h"
663dbc72 13#include "../h/vm.h"
973ecc4f 14#include "../h/trace.h"
d6d7360b 15#include "../h/uio.h"
663dbc72 16
663dbc72
BJ
17/*
18 * Swap IO headers -
19 * They contain the necessary information for the swap I/O.
20 * At any given time, a swap header can be in three
21 * different lists. When free it is in the free list,
22 * when allocated and the I/O queued, it is on the swap
23 * device list, and finally, if the operation was a dirty
24 * page push, when the I/O completes, it is inserted
25 * in a list of cleaned pages to be processed by the pageout daemon.
26 */
4c05b581
BJ
27struct buf *swbuf;
28short *swsize; /* CAN WE JUST USE B_BCOUNT? */
29int *swpf;
663dbc72 30
663dbc72
BJ
31/*
32 * swap I/O -
33 *
34 * If the flag indicates a dirty page push initiated
35 * by the pageout daemon, we map the page into the i th
36 * virtual page of process 2 (the daemon itself) where i is
37 * the index of the swap header that has been allocated.
38 * We simply initialize the header and queue the I/O but
39 * do not wait for completion. When the I/O completes,
40 * iodone() will link the header to a list of cleaned
41 * pages to be processed by the pageout daemon.
42 */
43swap(p, dblkno, addr, nbytes, rdflg, flag, dev, pfcent)
44 struct proc *p;
45 swblk_t dblkno;
46 caddr_t addr;
39d536e6 47 int nbytes, rdflg, flag;
663dbc72 48 dev_t dev;
39d536e6 49 u_int pfcent;
663dbc72
BJ
50{
51 register struct buf *bp;
e438ed8e 52 register u_int c;
663dbc72
BJ
53 int p2dp;
54 register struct pte *dpte, *vpte;
530d0032 55 int s;
663dbc72 56
530d0032 57 s = spl6();
663dbc72
BJ
58 while (bswlist.av_forw == NULL) {
59 bswlist.b_flags |= B_WANTED;
60 sleep((caddr_t)&bswlist, PSWP+1);
61 }
62 bp = bswlist.av_forw;
63 bswlist.av_forw = bp->av_forw;
530d0032 64 splx(s);
663dbc72
BJ
65
66 bp->b_flags = B_BUSY | B_PHYS | rdflg | flag;
67 if ((bp->b_flags & (B_DIRTY|B_PGIN)) == 0)
68 if (rdflg == B_READ)
69 sum.v_pswpin += btoc(nbytes);
70 else
71 sum.v_pswpout += btoc(nbytes);
72 bp->b_proc = p;
73 if (flag & B_DIRTY) {
74 p2dp = ((bp - swbuf) * CLSIZE) * KLMAX;
75 dpte = dptopte(&proc[2], p2dp);
76 vpte = vtopte(p, btop(addr));
77 for (c = 0; c < nbytes; c += NBPG) {
78 if (vpte->pg_pfnum == 0 || vpte->pg_fod)
79 panic("swap bad pte");
80 *dpte++ = *vpte++;
81 }
82 bp->b_un.b_addr = (caddr_t)ctob(p2dp);
83 } else
84 bp->b_un.b_addr = addr;
85 while (nbytes > 0) {
e438ed8e
BJ
86 bp->b_bcount = nbytes;
87 minphys(bp);
88 c = bp->b_bcount;
663dbc72
BJ
89 bp->b_blkno = dblkno;
90 bp->b_dev = dev;
d2f87136
BJ
91 if (flag & B_DIRTY) {
92 swpf[bp - swbuf] = pfcent;
93 swsize[bp - swbuf] = nbytes;
94 }
53f9ca20
BJ
95#ifdef TRACE
96 trace(TR_SWAPIO, dev, bp->b_blkno);
97#endif
ca1f746a 98 physstrat(bp, bdevsw[major(dev)].d_strategy, PSWP);
663dbc72
BJ
99 if (flag & B_DIRTY) {
100 if (c < nbytes)
101 panic("big push");
663dbc72
BJ
102 return;
103 }
663dbc72
BJ
104 bp->b_un.b_addr += c;
105 bp->b_flags &= ~B_DONE;
106 if (bp->b_flags & B_ERROR) {
107 if ((flag & (B_UAREA|B_PAGET)) || rdflg == B_WRITE)
108 panic("hard IO err in swap");
109 swkill(p, (char *)0);
110 }
111 nbytes -= c;
e438ed8e 112 dblkno += c / DEV_BSIZE;
663dbc72 113 }
530d0032 114 s = spl6();
663dbc72
BJ
115 bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS|B_PAGET|B_UAREA|B_DIRTY);
116 bp->av_forw = bswlist.av_forw;
117 bswlist.av_forw = bp;
118 if (bswlist.b_flags & B_WANTED) {
119 bswlist.b_flags &= ~B_WANTED;
120 wakeup((caddr_t)&bswlist);
121 wakeup((caddr_t)&proc[2]);
122 }
530d0032 123 splx(s);
663dbc72
BJ
124}
125
126/*
127 * If rout == 0 then killed on swap error, else
128 * rout is the name of the routine where we ran out of
129 * swap space.
130 */
131swkill(p, rout)
132 struct proc *p;
133 char *rout;
134{
444f631c 135 char *mesg;
663dbc72 136
444f631c 137 printf("pid %d: ", p->p_pid);
663dbc72 138 if (rout)
444f631c 139 printf(mesg = "killed due to no swap space\n");
663dbc72 140 else
444f631c
BJ
141 printf(mesg = "killed on swap error\n");
142 uprintf("sorry, pid %d was %s", p->p_pid, mesg);
663dbc72
BJ
143 /*
144 * To be sure no looping (e.g. in vmsched trying to
145 * swap out) mark process locked in core (as though
146 * done by user) after killing it so noone will try
147 * to swap it out.
148 */
a30d2e97 149 psignal(p, SIGKILL);
663dbc72
BJ
150 p->p_flag |= SULOCK;
151}
152
663dbc72
BJ
153/*
154 * Raw I/O. The arguments are
155 * The strategy routine for the device
156 * A buffer, which will always be a special buffer
157 * header owned exclusively by the device for this purpose
158 * The device number
159 * Read/write flag
160 * Essentially all the work is computing physical addresses and
161 * validating them.
162 * If the user has the proper access privilidges, the process is
163 * marked 'delayed unlock' and the pages involved in the I/O are
164 * faulted and locked. After the completion of the I/O, the above pages
165 * are unlocked.
166 */
d6d7360b
BJ
167physio(strat, bp, dev, rw, mincnt, uio)
168 int (*strat)();
169 register struct buf *bp;
170 dev_t dev;
171 int rw;
172 unsigned (*mincnt)();
173 struct uio *uio;
663dbc72 174{
406ddcbe 175 register struct iovec *iov = uio->uio_iov;
663dbc72
BJ
176 register int c;
177 char *a;
d6d7360b 178 int s, error = 0;
663dbc72 179
d6d7360b 180nextiov:
406ddcbe 181 if (uio->uio_iovcnt == 0)
d6d7360b 182 return (0);
406ddcbe 183 if (useracc(iov->iov_base,(u_int)iov->iov_len,rw==B_READ?B_WRITE:B_READ) == NULL)
d6d7360b 184 return (EFAULT);
530d0032 185 s = spl6();
663dbc72
BJ
186 while (bp->b_flags&B_BUSY) {
187 bp->b_flags |= B_WANTED;
188 sleep((caddr_t)bp, PRIBIO+1);
189 }
ef3b3d5a 190 splx(s);
663dbc72
BJ
191 bp->b_error = 0;
192 bp->b_proc = u.u_procp;
d6d7360b
BJ
193 bp->b_un.b_addr = iov->iov_base;
194 while (iov->iov_len > 0) {
663dbc72
BJ
195 bp->b_flags = B_BUSY | B_PHYS | rw;
196 bp->b_dev = dev;
e438ed8e 197 bp->b_blkno = uio->uio_offset / DEV_BSIZE;
d6d7360b 198 bp->b_bcount = iov->iov_len;
663dbc72
BJ
199 (*mincnt)(bp);
200 c = bp->b_bcount;
201 u.u_procp->p_flag |= SPHYSIO;
202 vslock(a = bp->b_un.b_addr, c);
e438ed8e 203 physstrat(bp, strat, PRIBIO);
81263dba 204 (void) spl6();
663dbc72
BJ
205 vsunlock(a, c, rw);
206 u.u_procp->p_flag &= ~SPHYSIO;
207 if (bp->b_flags&B_WANTED)
208 wakeup((caddr_t)bp);
530d0032 209 splx(s);
d6d7360b 210 c -= bp->b_resid;
663dbc72 211 bp->b_un.b_addr += c;
d6d7360b
BJ
212 iov->iov_len -= c;
213 uio->uio_resid -= c;
214 uio->uio_offset += c;
961945a8
SL
215 /* temp kludge for tape drives */
216 if (bp->b_resid || bp->b_flags&B_ERROR)
52a593fa 217 break;
663dbc72
BJ
218 }
219 bp->b_flags &= ~(B_BUSY|B_WANTED|B_PHYS);
d6d7360b 220 error = geterror(bp);
961945a8
SL
221 /* temp kludge for tape drives */
222 if (bp->b_resid || error)
d6d7360b 223 return (error);
d6d7360b
BJ
224 uio->uio_iov++;
225 uio->uio_iovcnt--;
226 goto nextiov;
663dbc72
BJ
227}
228
663dbc72
BJ
229unsigned
230minphys(bp)
d6d7360b 231 struct buf *bp;
663dbc72
BJ
232{
233
2ec65c94
BJ
234 if (bp->b_bcount > 63 * 1024)
235 bp->b_bcount = 63 * 1024;
663dbc72
BJ
236}
237