static void msgringent_free (msgringent * ktmp) { int i; for(i=0; i<ktmp->body.n; i++) ppage_unpin (kva2pp ((u_long) ktmp->body.r[i].data)); if (ktmp->owner != NULL) ppage_unpin (kva2pp ((u_long) ktmp->owner)); free (ktmp); }
void disk_buf_free (struct buf *bp) { u_int datalen; u_int data, next; /* unpin pages */ if (bp->b_flags & B_SCSICMD) { struct scsicmd *scsicmd = (struct scsicmd *) bp->b_memaddr; data = (u_int) scsicmd->data_addr; datalen = scsicmd->datalen; free (scsicmd->scsi_cmd); free (scsicmd); } else { data = (u_int)bp->b_memaddr; datalen = bp->b_bcount; } while (datalen > 0) { if (bp->b_flags & B_BC_REQ) { ppage_unpin (kva2pp (data)); } else { struct Env *e; int r; e = env_id2env (bp->b_envid, &r); assert (e); /* XXX - what if an env with an outstanding request dies? */ ppage_unpin (&ppages[PGNO (*env_va2ptep (e, data))]); } /* go to start of next page of data */ next = (data & ~PGMASK) + NBPG; if (next - data >= datalen) break; assert (next > data); datalen -= next - data; data = next; } if (bp->b_resptr) { ppage_unpin (kva2pp ((u_int) bp->b_resptr)); } free (bp); }
int disk_prepare_bc_request (u_int devno, u_quad_t blkno, void *vaddr, u_int flags, int *resptr, struct buf **headbpp) { struct buf *bp; /* XXX - test for big blkno wraparound */ if ((devno >= si->si_ndisks) || (((blkno * NBPG) / si->si_disks[devno].d_bsize) >= si->si_disks[devno].d_size)) { warn ("disk_prepare_bc_request: invalid devno (%u) or blkno (%qu)", devno, blkno); return (-E_INVAL); } if (headbpp == NULL) { warn ("disk_prepare_bc_request: headbpp == NULL"); return (-E_INVAL); } bp = disk_buf_alloc(); if (!bp) return (-E_NO_MEM); bp->b_next = NULL; bp->b_sgnext = NULL; bp->b_flags = flags; bp->b_dev = devno; bp->b_blkno = (blkno * NBPG) / si->si_disks[devno].d_bsize; bp->b_bcount = NBPG; bp->b_sgtot = 0; bp->b_memaddr = vaddr; bp->b_envid = curenv->env_id; bp->b_resid = 0; bp->b_resptr = NULL; ppage_pin (kva2pp((u_int) vaddr)); if (*headbpp) { struct buf *tmpbp = *headbpp; while (tmpbp->b_flags & B_SCATGATH) { tmpbp = (struct buf *) tmpbp->b_sgnext; if (tmpbp == NULL) { warn ("disk_prepare_bc_request: bad scatter/gather list"); ppage_unpin (kva2pp((u_int) vaddr)); free (bp); return (-E_INVAL); } } /* XXX - test for big blkno wraparound */ if (bp->b_blkno != (tmpbp->b_blkno + (tmpbp->b_bcount / si->si_disks[bp->b_dev].d_bsize))) { warn ("disk_prepare_bc_request: noncontiguous requests " "(prevblk %qu, size %u, curblk %qu) can't be merged", tmpbp->b_blkno, tmpbp->b_bcount, bp->b_blkno); ppage_unpin (kva2pp((u_int) vaddr)); free (bp); return (-E_INVAL); } (*headbpp)->b_sgtot += NBPG; tmpbp->b_flags |= B_SCATGATH; tmpbp->b_sgnext = bp; } else { *headbpp = bp; bp->b_sgtot = NBPG; } if (resptr) { ppage_pin (kva2pp(((u_int) resptr))); } bp->b_resptr = resptr; return (0); }
/* * sys_disk_request * * Disk I/O without going through the buffer cache. * * xn_user is the name of a pxn that grants access to the disk * reqbp is a list of scatter/gather requests * k is which capability in the env should be checked * * permission is granted to perform the operation if: * 1) the blocks in reqbp are covered by the pxn * 2) the capability gives access to the pxn * */ int sys_disk_request (u_int sn, struct Xn_name *xn_user, struct buf *reqbp, u_int k) { struct Xn_name xn; struct Xn_xtnt xtnt; struct Pxn *pxn; cap c; int ret; int access; struct disk *di; int *resptr = NULL; u_int bcount = 0; struct buf *bp, *segbp, *nsegbp; int noncontigs = 0, nctemp; #ifdef MEASURE_DISK_TIMES disk_pctr_start = rdtsc(); #endif /* XXX - use PFM or copyin instead of isreadable_* */ /* bypass for direct scsi commands */ if (reqbp->b_flags & B_SCSICMD) { return sys_disk_scsicmd (sn, k, reqbp); } /* get the capability */ if ((ret = env_getcap (curenv, k, &c)) < 0) return ret; /* and the pxn */ copyin (xn_user, &xn, sizeof (xn)); if (! (pxn = lookup_pxn (&xn))) { warn ("sys_disk_request: no pxn found"); return (-E_NOT_FOUND); } /* XXX - do we need to check that this is a physical disk? */ /* get a refernce to the disk unit for this command */ di = &(si->si_disks[xn.xa_dev]); /* Iterate over the request list checking: -- if the request is transfering data to/from memory that this user can read/write. -- if the pxn and capability specified give access to these blocks */ for (segbp = reqbp; ; segbp = (struct buf *) segbp->b_sgnext) { if (! (isreadable_varange ((u_int)segbp, sizeof(struct buf)))) { warn ("sys_disk_request: bad reqbp (%p)", segbp); return (-E_FAULT); } if (segbp->b_flags & B_READ) { access = ACL_R; } else { access = ACL_W; } xtnt.xtnt_block = segbp->b_blkno; xtnt.xtnt_size = segbp->b_bcount / di->d_bsize; bcount += segbp->b_bcount; if (! pxn_authorizes_xtnt (pxn, &c, &xtnt, access, &ret)) { warn ("sys_disk_request: pxn/cap does not grant access to block(s)"); return ret; } if (! ((reqbp->b_flags & B_READ) ? iswriteable_varange : isreadable_varange) ((u_int) segbp->b_memaddr, segbp->b_bcount)) { warn ("sys_disk_request: bad b_memaddr: %p (b_bcount %d)", segbp->b_memaddr, segbp->b_bcount); return (-E_FAULT); } if (! (segbp->b_flags & B_SCATGATH)) { if (segbp->b_resptr) { resptr = segbp->b_resptr; if ((((u_int) resptr) % sizeof(u_int)) || !(isvawriteable (resptr))) { warn ("sys_disk_request: bad resptr (%p)", resptr); return (-E_FAULT); } resptr = (int *) pa2kva (va2pa (resptr)); } break; } } if ((reqbp->b_flags & B_SCATGATH) && bcount != reqbp->b_sgtot) { warn ("sys_disk_request: invalid scatter/gather, with total (%u) unequal " "to sum of parts (%u)", reqbp->b_sgtot, bcount); return (-E_INVAL); } /* are we done before we've started? */ if (bcount == 0) { if (resptr) (*resptr)--; return (0); } if (bcount & di->d_bmod) { warn ("sys_disk_request: bad bcount %u", bcount); return (-E_INVAL); } /* copy request into kernel buffer */ segbp = reqbp; nsegbp = NULL; reqbp = NULL; do { segbp->b_dev = di->d_id; bp = copy_and_pin(segbp, segbp->b_bcount, &nctemp); if (!bp) { warn ("sys_disk_request: could not copy_and_pin"); /* XXX - cleanup before returning */ return (-E_NO_MEM); } noncontigs += nctemp; if (nsegbp) nsegbp->b_sgnext = bp; if (!reqbp) reqbp = bp; if (noncontigs >= DISK_MAX_SCATTER) { warn ("sys_disk_request: would require too many scatter/gather entries " "(%d)", noncontigs); /* XXX - cleanup before returning */ return (-E_INVAL); } nsegbp = bp; segbp = segbp->b_sgnext; } while (nsegbp->b_flags & B_SCATGATH); nsegbp->b_resptr = resptr; if (resptr) ppage_pin (kva2pp((u_int) resptr)); /* call appropriate strategy routine */ di->d_strategy (reqbp); #ifdef MEASURE_DISK_TIMES disk_pctr_return = rdtsc(); #endif return (0); }
/* XXX - we should use copyin, etc, instead of isreadable_* so that user will get pagefaults he can handle transparently */ static int sys_disk_scsicmd (u_int sn, u_int k, struct buf *reqbp) { struct buf *bp; struct scsicmd *scsicmd = (struct scsicmd *) reqbp->b_memaddr; struct scsicmd *scsicmd2; int noncontigs; struct disk *di; /* must have root capability for system to do a raw SCSI command!! */ /* XXX -- later, if desired, deeper checking of validity can reduce */ /* this restriction... */ if (k >= curenv->env_clen || ! curenv->env_clist[k].c_valid) { warn ("sys_disk_scsicmd: bad capability number %u\n", k); return (-E_CAP_INVALID); } if (! cap_isroot(&curenv->env_clist[k])) { warn ("sys_disk_scsicmd: cap %u is not root capability for system\n", k); return (-E_CAP_INSUFF); } /* must be able to read the reqbp ... */ if (! (isreadable_varange ((u_int) reqbp, sizeof (struct buf)))) { warn ("sys_disk_scsicmd: bad reqbp (%p)", reqbp); return (-E_FAULT); } /* Should be a SCSICMD */ if (! (reqbp->b_flags & B_SCSICMD)) { warn ("sys_disk_scsicmd: not a B_SCSICMD\n"); return (-E_INVAL); } /* Must be proper environment */ if (reqbp->b_envid != curenv->env_id) { warn ("sys_disk_scsicmd: bad envid\n"); return (-E_INVAL); } /* no scatter/gather support for raw SCSI commands */ if (reqbp->b_flags & B_SCATGATH) { warn ("sys_disk_scsicmd: B_SCATGATH not allowed with B_SCSICMD\n"); return (-E_INVAL); } /* can't send request to non-existent disk... */ if (reqbp->b_dev >= si->si_ndevs) { warn ("sys_disk_scsicmd: there is no disk %u in system\n", reqbp->b_dev); return (-E_NOT_FOUND); } /* check that everything is readable */ if (! isreadable_varange ((u_int) reqbp->b_memaddr, sizeof (struct scsicmd))) { warn ("sys_disk_scsicmd: SCSI command description is not readable\n"); return (-E_FAULT); } if (! isreadable_varange ((u_int) scsicmd->scsi_cmd, scsicmd->cmdlen) ) { warn ("sys_disk_scsicmd: SCSI command itself is not readable\n"); return (-E_FAULT); } if (! isreadable_varange ((u_int)scsicmd->data_addr, scsicmd->datalen) ) { warn ("sys_disk_scsicmd: data area for SCSI command is not readable\n"); return (-E_FAULT); } /* length of SCSI command must not be greater than B_SCSICMD_MAXLEN */ if (scsicmd->cmdlen > B_SCSICMD_MAXLEN) { /* XXX - why do we compare scsicmd->cmdlen, but we print out reqbp->b_bcount? */ warn ("sys_disk_scsicmd: specified SCSI command too large (%d > %d)\n", reqbp->b_bcount, B_SCSICMD_MAXLEN); return (-E_INVAL); } /* copy the SCSI command to avoid sharing it with app */ bp = bp_copy (reqbp); if (bp == NULL) { warn ("sys_disk_scsicmd: kernel malloc for bp failed\n"); return (-E_NO_MEM); } bp->b_memaddr = malloc (sizeof (struct scsicmd)); if (bp->b_memaddr == NULL) { warn ("sys_disk_scsicmd: kernel malloc for scsicmd failed\n"); free (bp); return (-E_NO_MEM); } scsicmd2 = (struct scsicmd *) bp->b_memaddr; bcopy (scsicmd, scsicmd2, sizeof (struct scsicmd)); scsicmd2->scsi_cmd = (struct scsi_generic *) malloc (scsicmd->cmdlen); if (scsicmd2->scsi_cmd == NULL) { warn ("sys_disk_scsicmd: second kernel malloc failed\n"); free (bp->b_memaddr); free (bp); return (-E_NO_MEM); } bcopy (scsicmd->scsi_cmd, scsicmd2->scsi_cmd, scsicmd->cmdlen); scsicmd2->bp = bp; bp->b_resid = scsicmd->datalen; bp->b_resptr = (int *) pa2kva (va2pa (reqbp->b_resptr)); /* pin down the app pages that will later be used by the driver */ ppage_pin (kva2pp ((u_int) bp->b_resptr)); noncontigs = pin_and_count_noncontigs (scsicmd2->data_addr, scsicmd2->datalen); if (noncontigs >= DISK_MAX_SCATTER) { warn ("sys_disk_scsicmd: will require too many scatter/gather entries " "(%d)", noncontigs); disk_buf_free (bp); return (-E_TOO_BIG); } /* XXX */ /* call down to the low-level driver. GROK -- since the partition stuff */ /* creates and abstract disk that is separate from the real one, a hack */ /* is needed to get the actual disk strategy routine for raw SCSI commands */ /* This is fine as long as all disks actually go to the same strategy */ /* routine. */ di = &(si->si_disks[0]); di->d_strategy (bp); return (0); }
static msgringent * msgringent_setup (msgringent * u_msgringent) { msgringent *ktmp; Pte *pte = NULL; int scatptr = 0; int total_len = 0; ktmp = (msgringent *) malloc (sizeof (msgringent)); if (ktmp == NULL) { warn ("msgringent_setup: failed malloc"); return (NULL); } ktmp->appaddr = u_msgringent; ktmp->owner = NULL; ktmp->body.n = 0; /* Verify and translate owner field */ if ((((u_int) u_msgringent->owner % sizeof (int)) || ! (pte = va2ptep ((u_int) u_msgringent->owner)) || ((*pte & WRITE_MASK) != WRITE_MASK))) { warn ("msgringent_setup: owner field failed\n"); msgringent_free (ktmp); return (NULL); } ktmp->owner = (u_int *) pa2kva (va2pa (u_msgringent->owner)); ppage_pin (kva2pp ((u_long) ktmp->owner)); /* Verify and translate data field */ if (u_msgringent->body.n > 1) { warn ("msgringent_setup: not allowed to setup disjoint message body\n"); msgringent_free (ktmp); return (NULL); } scatptr = 0; total_len = 0; { int len = u_msgringent->body.r[0].sz; caddr_t addr = u_msgringent->body.r[0].data; u_int pagebound = NBPG-(((u_long)addr)&(NBPG - 1)); while (len > 0) { u_int slen = min (len, pagebound); if (!(pte = va2ptep ((u_int) addr)) || ((*pte & READ_MASK) != READ_MASK)) { /* physical page is not accessible */ warn ("msgringent_setup: can't read scatter ptr\n"); msgringent_free (ktmp); return (NULL); } ktmp->body.r[scatptr].data = (char *) pa2kva (va2pa (addr)); ktmp->body.r[scatptr].sz = slen; ktmp->body.n++; /* pin the page to prevent re-allocation */ ppage_pin (kva2pp ((u_long) ktmp->body.r[scatptr].data)); len -= slen; addr += slen; total_len += slen; pagebound = NBPG; scatptr++; if (scatptr > IPC_MAX_SCATTER_PTR || total_len > IPC_MAX_MSG_SIZE) { msgringent_free (ktmp); warn ("msgringent_setup: message body too big\n"); return (NULL); } } } return (ktmp); }