modules/examples/fopsmapper: KNF, sprinkle comments
Missing: MP-safety; this is a kind of broken example.
This commit is contained in:
parent
01905a679e
commit
212c9b3b52
|
@ -1,4 +1,4 @@
|
||||||
/* $NetBSD: cmd_mapper.c,v 1.2 2020/04/01 13:07:32 kamil Exp $ */
|
/* $NetBSD: cmd_mapper.c,v 1.3 2024/04/17 18:10:27 riastradh Exp $ */
|
||||||
|
|
||||||
/*-
|
/*-
|
||||||
* Copyright (c) 2020 The NetBSD Foundation, Inc.
|
* Copyright (c) 2020 The NetBSD Foundation, Inc.
|
||||||
|
@ -27,7 +27,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
__RCSID("$NetBSD: cmd_mapper.c,v 1.2 2020/04/01 13:07:32 kamil Exp $");
|
__RCSID("$NetBSD: cmd_mapper.c,v 1.3 2024/04/17 18:10:27 riastradh Exp $");
|
||||||
|
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
|
||||||
|
@ -47,8 +47,7 @@ int main(int argc, char **argv)
|
||||||
if ((devfd = open(_PATH_DEV_MAPPER, O_RDONLY)) == -1)
|
if ((devfd = open(_PATH_DEV_MAPPER, O_RDONLY)) == -1)
|
||||||
err(EXIT_FAILURE, "Cannot open %s", _PATH_DEV_MAPPER);
|
err(EXIT_FAILURE, "Cannot open %s", _PATH_DEV_MAPPER);
|
||||||
|
|
||||||
map = (char *)(mmap(0, sysconf(_SC_PAGESIZE), PROT_READ, MAP_SHARED,
|
map = mmap(0, sysconf(_SC_PAGESIZE), PROT_READ, MAP_SHARED, devfd, 0);
|
||||||
devfd, 0));
|
|
||||||
if (map == MAP_FAILED)
|
if (map == MAP_FAILED)
|
||||||
err(EXIT_FAILURE, "Mapping failed");
|
err(EXIT_FAILURE, "Mapping failed");
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
/* $NetBSD: fopsmapper.c,v 1.2 2020/04/01 11:45:53 kamil Exp $ */
|
/* $NetBSD: fopsmapper.c,v 1.3 2024/04/17 18:10:27 riastradh Exp $ */
|
||||||
|
|
||||||
/*-
|
/*-
|
||||||
* Copyright (c) 2020 The NetBSD Foundation, Inc.
|
* Copyright (c) 2020 The NetBSD Foundation, Inc.
|
||||||
|
@ -27,29 +27,30 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
__KERNEL_RCSID(0, "$NetBSD: fopsmapper.c,v 1.2 2020/04/01 11:45:53 kamil Exp $");
|
__KERNEL_RCSID(0, "$NetBSD: fopsmapper.c,v 1.3 2024/04/17 18:10:27 riastradh Exp $");
|
||||||
|
|
||||||
#include <sys/module.h>
|
#include <sys/types.h>
|
||||||
#include <sys/param.h>
|
|
||||||
#include <sys/systm.h>
|
|
||||||
#include <sys/kernel.h>
|
|
||||||
|
|
||||||
#include <sys/conf.h>
|
#include <sys/conf.h>
|
||||||
#include <sys/file.h>
|
#include <sys/file.h>
|
||||||
#include <sys/filedesc.h>
|
#include <sys/filedesc.h>
|
||||||
|
#include <sys/kernel.h>
|
||||||
#include <sys/kmem.h>
|
#include <sys/kmem.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
|
#include <sys/module.h>
|
||||||
#include <sys/mutex.h>
|
#include <sys/mutex.h>
|
||||||
|
#include <sys/param.h>
|
||||||
|
#include <sys/systm.h>
|
||||||
|
|
||||||
#include <uvm/uvm_extern.h>
|
#include <uvm/uvm_extern.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* To use this module you need to:
|
* To use this module you need to:
|
||||||
*
|
*
|
||||||
* mknod /dev/fopsmapper c 351 0
|
* mknod /dev/fopsmapper c 351 0
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
dev_type_open(fopsmapper_open);
|
dev_open_t fopsmapper_open;
|
||||||
|
|
||||||
const struct cdevsw fopsmapper_cdevsw = {
|
const struct cdevsw fopsmapper_cdevsw = {
|
||||||
.d_open = fopsmapper_open,
|
.d_open = fopsmapper_open,
|
||||||
|
@ -66,9 +67,9 @@ const struct cdevsw fopsmapper_cdevsw = {
|
||||||
.d_flag = D_OTHER
|
.d_flag = D_OTHER
|
||||||
};
|
};
|
||||||
|
|
||||||
static int fopsmapper_mmap(file_t *, off_t *, size_t,
|
static int fopsmapper_mmap(struct file *, off_t *, size_t, int, int *, int *,
|
||||||
int, int *, int *,struct uvm_object **, int *);
|
struct uvm_object **, int *);
|
||||||
static int fopsmapper_close(file_t *);
|
static int fopsmapper_close(struct file *);
|
||||||
|
|
||||||
const struct fileops mapper_fileops = {
|
const struct fileops mapper_fileops = {
|
||||||
.fo_read = fbadop_read,
|
.fo_read = fbadop_read,
|
||||||
|
@ -83,16 +84,16 @@ const struct fileops mapper_fileops = {
|
||||||
.fo_mmap = fopsmapper_mmap,
|
.fo_mmap = fopsmapper_mmap,
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef struct fopsmapper_softc {
|
struct fopsmapper_softc {
|
||||||
char *buf;
|
char *buf;
|
||||||
struct uvm_object *uobj;
|
struct uvm_object *uobj;
|
||||||
size_t bufsize;
|
size_t bufsize;
|
||||||
} fops_t;
|
};
|
||||||
|
|
||||||
int
|
int
|
||||||
fopsmapper_open(dev_t dev, int flag, int mode, struct lwp *l)
|
fopsmapper_open(dev_t dev, int flag, int mode, struct lwp *l)
|
||||||
{
|
{
|
||||||
fops_t *fo;
|
struct fopsmapper_softc *fo;
|
||||||
struct file *fp;
|
struct file *fp;
|
||||||
int fd, error;
|
int fd, error;
|
||||||
|
|
||||||
|
@ -105,11 +106,11 @@ fopsmapper_open(dev_t dev, int flag, int mode, struct lwp *l)
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
fopsmapper_mmap(file_t * fp, off_t * offp, size_t size, int prot,
|
fopsmapper_mmap(struct file *fp, off_t *offp, size_t size, int prot,
|
||||||
int *flagsp, int *advicep, struct uvm_object **uobjp,
|
int *flagsp, int *advicep, struct uvm_object **uobjp, int *maxprotp)
|
||||||
int *maxprotp)
|
|
||||||
{
|
{
|
||||||
fops_t *fo;
|
struct fopsmapper_softc *fo;
|
||||||
|
vaddr_t va;
|
||||||
int error;
|
int error;
|
||||||
|
|
||||||
if (prot & PROT_EXEC)
|
if (prot & PROT_EXEC)
|
||||||
|
@ -125,32 +126,35 @@ fopsmapper_mmap(file_t * fp, off_t * offp, size_t size, int prot,
|
||||||
fo->uobj = uao_create(size, 0);
|
fo->uobj = uao_create(size, 0);
|
||||||
|
|
||||||
fo->buf = NULL;
|
fo->buf = NULL;
|
||||||
/* Map the uvm object into kernel */
|
|
||||||
error = uvm_map(kernel_map, (vaddr_t *) &fo->buf, fo->bufsize,
|
/*
|
||||||
fo->uobj, 0, 0,
|
* Map the uvm object into kernel. Consumes our reference to
|
||||||
|
* uobj on success.
|
||||||
|
*/
|
||||||
|
error = uvm_map(kernel_map, &va, fo->bufsize, fo->uobj, 0, 0,
|
||||||
UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
|
UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
|
||||||
UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
|
UVM_INH_SHARE, UVM_ADV_RANDOM, 0));
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
uao_detach(fo->uobj);
|
uao_detach(fo->uobj);
|
||||||
return error;
|
goto out;
|
||||||
}
|
}
|
||||||
|
fo->buf = (char *)va;
|
||||||
snprintf(fo->buf, 13, "Hey There!");
|
snprintf(fo->buf, 13, "Hey There!");
|
||||||
|
|
||||||
/* Get the reference of uobj */
|
/* Get the reference of uobj */
|
||||||
uao_reference(fo->uobj);
|
uao_reference(fo->uobj);
|
||||||
|
|
||||||
*uobjp = fo->uobj;
|
*uobjp = fo->uobj;
|
||||||
*maxprotp = prot;
|
*maxprotp = prot;
|
||||||
*advicep = UVM_ADV_RANDOM;
|
*advicep = UVM_ADV_RANDOM;
|
||||||
|
error = 0;
|
||||||
|
|
||||||
return 0;
|
out: return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
fopsmapper_close(file_t * fp)
|
fopsmapper_close(struct file *fp)
|
||||||
{
|
{
|
||||||
fops_t *fo;
|
struct fopsmapper_softc *fo;
|
||||||
|
|
||||||
fo = fp->f_data;
|
fo = fp->f_data;
|
||||||
KASSERT(fo != NULL);
|
KASSERT(fo != NULL);
|
||||||
|
|
Loading…
Reference in New Issue