added recent changes
This commit is contained in:
@@ -1,518 +1,59 @@
|
||||
/* SPDX-License-Identifier: BSD-3-Clause
|
||||
* Copyright(c) 2010-2014 Intel Corporation
|
||||
*/
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
__FBSDID("$FreeBSD$");
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/bio.h>
|
||||
#include <sys/bus.h>
|
||||
#include <sys/conf.h>
|
||||
#include <sys/module.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/module.h>
|
||||
#include <sys/proc.h>
|
||||
#include <sys/lock.h>
|
||||
#include <sys/rwlock.h>
|
||||
#include <sys/mutex.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/vmmeter.h>
|
||||
#include <sys/eventhandler.h>
|
||||
|
||||
#include <machine/bus.h>
|
||||
|
||||
#include <vm/vm.h>
|
||||
#include <vm/pmap.h>
|
||||
#include <vm/vm_param.h>
|
||||
#include <vm/vm_object.h>
|
||||
#include <vm/vm_extern.h>
|
||||
#include <vm/vm_page.h>
|
||||
#include <vm/vm_pager.h>
|
||||
#include <vm/vm_phys.h>
|
||||
#include <vm/vm_kern.h>
|
||||
|
||||
// #include <cheriintrin.h>
|
||||
#include <cheri/cheric.h>
|
||||
#define HUGE_PAGE_SIZE (2 * 1024 * 1024) // 2MB Huge Page
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
// #include <fcntl.h>
|
||||
static void *hugepage_ptr = NULL;
|
||||
|
||||
// #include <assert.h>
|
||||
// #include <stdlib.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/errno.h>
|
||||
// #include <stdint.h>
|
||||
// #include <stdio.h>
|
||||
// #include <unistd.h>
|
||||
// Load function - allocate memory
|
||||
static int load_handler(module_t mod, int event, void *arg) {
|
||||
switch (event) {
|
||||
case MOD_LOAD:
|
||||
printf("Loading Huge Page Kernel Module...\n");
|
||||
|
||||
// Allocate contiguous memory
|
||||
hugepage_ptr = kmem_alloc_contig(HUGE_PAGE_SIZE, M_NOWAIT,
|
||||
VM_MIN_KERNEL_ADDRESS, VM_MAX_KERNEL_ADDRESS,
|
||||
HUGE_PAGE_SIZE, 0,
|
||||
VM_MEMATTR_DEFAULT);
|
||||
|
||||
// #include <stddef.h>
|
||||
// #include <stdlib.h>
|
||||
// #include <string.h>
|
||||
if (hugepage_ptr == NULL) {
|
||||
printf("Failed to allocate a huge page!\n");
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
// added to print uint
|
||||
// 64
|
||||
// #include <inttypes.h>
|
||||
printf("Huge page allocated at %p\n", hugepage_ptr);
|
||||
break;
|
||||
|
||||
struct contigmem_buffer {
|
||||
void *addr;
|
||||
int refcnt;
|
||||
struct mtx mtx;
|
||||
};
|
||||
case MOD_UNLOAD:
|
||||
printf("Unloading Huge Page Kernel Module...\n");
|
||||
|
||||
struct contigmem_vm_handle {
|
||||
int buffer_index;
|
||||
};
|
||||
// Free memory if allocated
|
||||
if (hugepage_ptr) {
|
||||
kmem_free(hugepage_ptr, HUGE_PAGE_SIZE);
|
||||
printf("Huge page freed.\n");
|
||||
}
|
||||
break;
|
||||
|
||||
static int contigmem_load(void);
|
||||
static int contigmem_unload(void);
|
||||
static int contigmem_physaddr(SYSCTL_HANDLER_ARGS);
|
||||
|
||||
static d_mmap_single_t contigmem_mmap_single;
|
||||
static d_open_t contigmem_open;
|
||||
static d_close_t contigmem_close;
|
||||
|
||||
static int contigmem_num_buffers = RTE_CONTIGMEM_DEFAULT_NUM_BUFS;
|
||||
static int64_t contigmem_buffer_size = RTE_CONTIGMEM_DEFAULT_BUF_SIZE;
|
||||
|
||||
static eventhandler_tag contigmem_eh_tag;
|
||||
static struct contigmem_buffer contigmem_buffers[RTE_CONTIGMEM_MAX_NUM_BUFS];
|
||||
static struct cdev *contigmem_cdev = NULL;
|
||||
static int contigmem_refcnt;
|
||||
|
||||
TUNABLE_INT("hw.contigmem.num_buffers", &contigmem_num_buffers);
|
||||
TUNABLE_QUAD("hw.contigmem.buffer_size", &contigmem_buffer_size);
|
||||
|
||||
static SYSCTL_NODE(_hw, OID_AUTO, contigmem, CTLFLAG_RD, 0, "contigmem");
|
||||
|
||||
SYSCTL_INT(_hw_contigmem, OID_AUTO, num_buffers, CTLFLAG_RD,
|
||||
&contigmem_num_buffers, 0, "Number of contigmem buffers allocated");
|
||||
SYSCTL_QUAD(_hw_contigmem, OID_AUTO, buffer_size, CTLFLAG_RD,
|
||||
&contigmem_buffer_size, 0, "Size of each contiguous buffer");
|
||||
SYSCTL_INT(_hw_contigmem, OID_AUTO, num_references, CTLFLAG_RD,
|
||||
&contigmem_refcnt, 0, "Number of references to contigmem");
|
||||
|
||||
static SYSCTL_NODE(_hw_contigmem, OID_AUTO, physaddr, CTLFLAG_RD, 0,
|
||||
"physaddr");
|
||||
|
||||
MALLOC_DEFINE(M_CONTIGMEM, "customcontigmem", "customcontigmem(4) allocations");
|
||||
|
||||
void *ptr;
|
||||
int MallocCounter;
|
||||
|
||||
#define MAXPAGESIZES 2
|
||||
|
||||
|
||||
/*
|
||||
* The offset in sysent where the syscall is allocated.
|
||||
*/
|
||||
static int offset = NO_SYSCALL;
|
||||
|
||||
static int contigmem_modevent(module_t mod, int type, void *arg)
|
||||
{
|
||||
int error = 0;
|
||||
|
||||
switch (type) {
|
||||
case MOD_LOAD:
|
||||
error = contigmem_load();
|
||||
break;
|
||||
case MOD_UNLOAD:
|
||||
error = contigmem_unload();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
moduledata_t contigmem_mod = {
|
||||
"contigmem",
|
||||
(modeventhand_t)contigmem_modevent,
|
||||
0
|
||||
};
|
||||
|
||||
DECLARE_MODULE(contigmem, contigmem_mod, SI_SUB_DRIVERS, SI_ORDER_ANY);
|
||||
MODULE_VERSION(contigmem, 1);
|
||||
|
||||
static struct cdevsw contigmem_ops = {
|
||||
.d_name = "contigmem",
|
||||
.d_version = D_VERSION,
|
||||
.d_flags = D_TRACKCLOSE,
|
||||
.d_mmap_single = contigmem_mmap_single,
|
||||
.d_open = contigmem_open,
|
||||
.d_close = contigmem_close,
|
||||
};
|
||||
|
||||
static int
|
||||
pagesizes(size_t ps[MAXPAGESIZES])
|
||||
{
|
||||
int pscnt;
|
||||
|
||||
pscnt = getpagesizes(ps, MAXPAGESIZES);
|
||||
// ATF_REQUIRE_MSG(pscnt != -1, "getpagesizes failed; errno=%d", errno);
|
||||
// ATF_REQUIRE_MSG(ps[0] != 0, "psind 0 is %zu", ps[0]);
|
||||
// ATF_REQUIRE_MSG(pscnt <= MAXPAGESIZES, "invalid pscnt %d", pscnt);
|
||||
// if (pscnt == 1){
|
||||
// printf("pscnt is 1");
|
||||
// }
|
||||
|
||||
// atf_tc_skip("no large page support");
|
||||
return (pscnt);
|
||||
}
|
||||
|
||||
static int
|
||||
contigmem_load()
|
||||
{
|
||||
|
||||
size_t sz;
|
||||
// Pre Allocate 1GB huge page
|
||||
sz = 1073741824;
|
||||
|
||||
int error, fd, pscnt, pn;
|
||||
|
||||
size_t ps[MAXPAGESIZES];
|
||||
|
||||
size_t size[3];
|
||||
|
||||
pn = getpagesizes(size, 3);
|
||||
printf("page size is [%d]", size[2]);
|
||||
|
||||
pscnt = pagesizes(ps);
|
||||
|
||||
fd = shm_create_largepage(SHM_ANON, O_CREAT | O_RDWR, 1, SHM_LARGEPAGE_ALLOC_DEFAULT, 0);
|
||||
|
||||
if (fd < 0 && errno == ENOTTY) {
|
||||
// perror("sh_create_largepages");
|
||||
close(fd);
|
||||
}
|
||||
// if (fd < 0)
|
||||
// perror("no large page supported");
|
||||
// exit(EXIT_FAILURE);
|
||||
// if (fd < 0 && errno == ENOTTY)
|
||||
// atf_tc_skip("no large page support");
|
||||
// ATF_REQUIRE_MSG(fd >= 0, "shm_create_largepage failed; errno=%d", errno);
|
||||
|
||||
if (ftruncate(fd, sz) < 0) {
|
||||
// perror("ftruncate");
|
||||
close(fd);
|
||||
default:
|
||||
return EOPNOTSUPP;
|
||||
}
|
||||
// if (error != 0 && errno == ENOMEM)
|
||||
// /*
|
||||
// * The test system might not have enough memory to accommodate
|
||||
// * the request.
|
||||
// */
|
||||
// atf_tc_skip("failed to allocate %zu-byte superpage", sz);
|
||||
// ATF_REQUIRE_MSG(error == 0, "ftruncate failed; errno=%d", errno);
|
||||
|
||||
ptr = mmap(NULL, sz,
|
||||
PROT_READ|PROT_WRITE, MAP_SHARED,fd,0);
|
||||
|
||||
// Added error handling
|
||||
if(ptr == MAP_FAILED)
|
||||
{
|
||||
// perror("mmap");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
MallocCounter = (int)sz;
|
||||
|
||||
return 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
contigmem_unload()
|
||||
{
|
||||
// int i;
|
||||
|
||||
// if (contigmem_refcnt > 0)
|
||||
// return EBUSY;
|
||||
|
||||
// if (contigmem_cdev != NULL)
|
||||
// destroy_dev(contigmem_cdev);
|
||||
|
||||
// if (contigmem_eh_tag != NULL)
|
||||
// EVENTHANDLER_DEREGISTER(process_exit, contigmem_eh_tag);
|
||||
|
||||
// for (i = 0; i < RTE_CONTIGMEM_MAX_NUM_BUFS; i++) {
|
||||
// if (contigmem_buffers[i].addr != NULL)
|
||||
// contigfree(contigmem_buffers[i].addr,
|
||||
// contigmem_buffer_size, M_CONTIGMEM);
|
||||
// if (mtx_initialized(&contigmem_buffers[i].mtx))
|
||||
// mtx_destroy(&contigmem_buffers[i].mtx);
|
||||
// }
|
||||
|
||||
// printf("free called \n");
|
||||
|
||||
// get bounds from
|
||||
|
||||
// printf("free len %d \n", len);
|
||||
|
||||
// Free the entire huge page
|
||||
|
||||
munmap(ptr, 1073741824);
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
contigmem_physaddr(SYSCTL_HANDLER_ARGS)
|
||||
{
|
||||
uint64_t physaddr;
|
||||
int index = (int)(uintptr_t)arg1;
|
||||
|
||||
physaddr = (uint64_t)vtophys(contigmem_buffers[index].addr);
|
||||
return sysctl_handle_64(oidp, &physaddr, 0, req);
|
||||
}
|
||||
|
||||
static int
|
||||
contigmem_open(struct cdev *cdev, int fflags, int devtype,
|
||||
struct thread *td)
|
||||
{
|
||||
|
||||
printf("Contigmem opened \n");
|
||||
|
||||
atomic_add_int(&contigmem_refcnt, 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
contigmem_close(struct cdev *cdev, int fflags, int devtype,
|
||||
struct thread *td)
|
||||
{
|
||||
|
||||
atomic_subtract_int(&contigmem_refcnt, 1);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
contigmem_cdev_pager_ctor(void *handle, vm_ooffset_t size, vm_prot_t prot,
|
||||
vm_ooffset_t foff, struct ucred *cred, u_short *color)
|
||||
{
|
||||
struct contigmem_vm_handle *vmh = handle;
|
||||
struct contigmem_buffer *buf;
|
||||
|
||||
buf = &contigmem_buffers[vmh->buffer_index];
|
||||
|
||||
atomic_add_int(&contigmem_refcnt, 1);
|
||||
|
||||
mtx_lock(&buf->mtx);
|
||||
if (buf->refcnt == 0)
|
||||
memset(buf->addr, 0, contigmem_buffer_size);
|
||||
buf->refcnt++;
|
||||
mtx_unlock(&buf->mtx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
contigmem_cdev_pager_dtor(void *handle)
|
||||
{
|
||||
struct contigmem_vm_handle *vmh = handle;
|
||||
struct contigmem_buffer *buf;
|
||||
|
||||
buf = &contigmem_buffers[vmh->buffer_index];
|
||||
|
||||
mtx_lock(&buf->mtx);
|
||||
buf->refcnt--;
|
||||
mtx_unlock(&buf->mtx);
|
||||
|
||||
free(vmh, M_CONTIGMEM);
|
||||
|
||||
atomic_subtract_int(&contigmem_refcnt, 1);
|
||||
}
|
||||
|
||||
static int
|
||||
contigmem_cdev_pager_fault(vm_object_t object, vm_ooffset_t offset, int prot,
|
||||
vm_page_t *mres)
|
||||
{
|
||||
vm_paddr_t paddr;
|
||||
vm_page_t m_paddr, page;
|
||||
vm_memattr_t memattr, memattr1;
|
||||
|
||||
memattr = object->memattr;
|
||||
|
||||
VM_OBJECT_WUNLOCK(object);
|
||||
|
||||
paddr = offset;
|
||||
|
||||
m_paddr = vm_phys_paddr_to_vm_page(paddr);
|
||||
if (m_paddr != NULL) {
|
||||
memattr1 = pmap_page_get_memattr(m_paddr);
|
||||
if (memattr1 != memattr)
|
||||
memattr = memattr1;
|
||||
}
|
||||
|
||||
if (((*mres)->flags & PG_FICTITIOUS) != 0) {
|
||||
/*
|
||||
* If the passed in result page is a fake page, update it with
|
||||
* the new physical address.
|
||||
*/
|
||||
page = *mres;
|
||||
VM_OBJECT_WLOCK(object);
|
||||
vm_page_updatefake(page, paddr, memattr);
|
||||
} else {
|
||||
/*
|
||||
* Replace the passed in reqpage page with our own fake page and
|
||||
* free up the original page.
|
||||
*/
|
||||
page = vm_page_getfake(paddr, memattr);
|
||||
VM_OBJECT_WLOCK(object);
|
||||
#if __FreeBSD__ >= 13
|
||||
vm_page_replace(page, object, (*mres)->pindex, *mres);
|
||||
#else
|
||||
vm_page_t mret = vm_page_replace(page, object, (*mres)->pindex);
|
||||
KASSERT(mret == *mres,
|
||||
("invalid page replacement, old=%p, ret=%p", *mres, mret));
|
||||
vm_page_lock(mret);
|
||||
vm_page_free(mret);
|
||||
vm_page_unlock(mret);
|
||||
#endif
|
||||
*mres = page;
|
||||
}
|
||||
|
||||
page->valid = VM_PAGE_BITS_ALL;
|
||||
|
||||
return VM_PAGER_OK;
|
||||
}
|
||||
|
||||
static struct cdev_pager_ops contigmem_cdev_pager_ops = {
|
||||
.cdev_pg_ctor = contigmem_cdev_pager_ctor,
|
||||
.cdev_pg_dtor = contigmem_cdev_pager_dtor,
|
||||
.cdev_pg_fault = contigmem_cdev_pager_fault,
|
||||
// Register module
|
||||
static moduledata_t superpage_mod = {
|
||||
"superpage", // Module name
|
||||
load_handler,
|
||||
NULL
|
||||
};
|
||||
|
||||
static int
|
||||
(struct cdev *cdev, vm_ooffset_t *offset, vm_size_t size,
|
||||
struct vm_object **obj, int nprot)
|
||||
{
|
||||
DECLARE_MODULE(superpage, superpage_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
|
||||
|
||||
// Testing if this is called when file is opened
|
||||
printf("contigmem_mmap_single called \n");
|
||||
|
||||
// struct contigmem_vm_handle *vmh;
|
||||
// uint64_t buffer_index;
|
||||
|
||||
// /*
|
||||
// * The buffer index is encoded in the offset. Divide the offset by
|
||||
// * PAGE_SIZE to get the index of the buffer requested by the user
|
||||
// * app.
|
||||
// */
|
||||
// buffer_index = *offset / PAGE_SIZE;
|
||||
// if (buffer_index >= contigmem_num_buffers)
|
||||
// return EINVAL;
|
||||
|
||||
// if (size > contigmem_buffer_size)
|
||||
// return EINVAL;
|
||||
|
||||
// vmh = malloc(sizeof(*vmh), M_CONTIGMEM, M_NOWAIT | M_ZERO);
|
||||
// if (vmh == NULL)
|
||||
// return ENOMEM;
|
||||
// vmh->buffer_index = buffer_index;
|
||||
|
||||
// *offset = (vm_ooffset_t)vtophys(contigmem_buffers[buffer_index].addr);
|
||||
// *obj = cdev_pager_allocate(vmh, OBJT_DEVICE, &contigmem_cdev_pager_ops,
|
||||
// size, nprot, *offset, curthread->td_ucred);
|
||||
|
||||
size = __builtin_align_up(size, _Alignof(max_align_t));
|
||||
|
||||
// printf("%d \n", sz);
|
||||
// printf("%d Malloc counter\n", MallocCounter);
|
||||
|
||||
MallocCounter -= size;
|
||||
obj = &ptr[MallocCounter];
|
||||
obj = cheri_setbounds(obj, sz);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
// SAMPLE SYSCALL IMPLEMENTATION
|
||||
// /*-
|
||||
// * SPDX-License-Identifier: BSD-2-Clause
|
||||
// *
|
||||
// * Copyright (c) 1999 Assar Westerlund
|
||||
// * All rights reserved.
|
||||
// *
|
||||
// * Redistribution and use in source and binary forms, with or without
|
||||
// * modification, are permitted provided that the following conditions
|
||||
// * are met:
|
||||
// * 1. Redistributions of source code must retain the above copyright
|
||||
// * notice, this list of conditions and the following disclaimer.
|
||||
// * 2. Redistributions in binary form must reproduce the above copyright
|
||||
// * notice, this list of conditions and the following disclaimer in the
|
||||
// * documentation and/or other materials provided with the distribution.
|
||||
// *
|
||||
// * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
// * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
// * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
// * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
// * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
// * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
// * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
// * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
// * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
// * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
// * SUCH DAMAGE.
|
||||
// */
|
||||
|
||||
// #include <sys/param.h>
|
||||
// #include <sys/proc.h>
|
||||
// #include <sys/module.h>
|
||||
// #include <sys/sysproto.h>
|
||||
// #include <sys/sysent.h>
|
||||
// #include <sys/kernel.h>
|
||||
// #include <sys/systm.h>
|
||||
|
||||
// /*
|
||||
// * The function for implementing the syscall.
|
||||
// */
|
||||
// static int
|
||||
// hello(struct thread *td, void *arg)
|
||||
// {
|
||||
|
||||
// printf("hello kernel\n");
|
||||
// return (0);
|
||||
// }
|
||||
|
||||
// /*
|
||||
// * The `sysent' for the new syscall
|
||||
// */
|
||||
// static struct sysent hello_sysent = {
|
||||
// .sy_narg = 0,
|
||||
// .sy_call = hello
|
||||
// };
|
||||
|
||||
// /*
|
||||
// * The offset in sysent where the syscall is allocated.
|
||||
// */
|
||||
// static int offset = NO_SYSCALL;
|
||||
|
||||
// /*
|
||||
// * The function called at load/unload.
|
||||
// */
|
||||
// static int
|
||||
// load(struct module *module, int cmd, void *arg)
|
||||
// {
|
||||
// int error = 0;
|
||||
|
||||
// switch (cmd) {
|
||||
// case MOD_LOAD :
|
||||
// printf("syscall loaded at %d\n", offset);
|
||||
// break;
|
||||
// case MOD_UNLOAD :
|
||||
// printf("syscall unloaded from %d\n", offset);
|
||||
// break;
|
||||
// default :
|
||||
// error = EOPNOTSUPP;
|
||||
// break;
|
||||
// }
|
||||
// return (error);
|
||||
// }
|
||||
|
||||
// SYSCALL_MODULE(syscall, &offset, &hello_sysent, load, NULL);
|
||||
@@ -1,313 +1,78 @@
|
||||
/*-
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*
|
||||
* Copyright (c) 1999 Assar Westerlund
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
#include <sys/param.h>
|
||||
#include <sys/module.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/systm.h>
|
||||
#include <vm/vm.h>
|
||||
#include <vm/vm_extern.h>
|
||||
#include <vm/vm_page.h>
|
||||
#include <vm/vm_kern.h>
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/proc.h>
|
||||
#include <sys/module.h>
|
||||
#include <sys/sysproto.h>
|
||||
#include <sys/sysent.h>
|
||||
#include <sys/bio.h>
|
||||
#include <sys/bus.h>
|
||||
#include <sys/conf.h>
|
||||
#include <sys/kernel.h>
|
||||
#include <sys/systm.h>
|
||||
|
||||
#include <sys/malloc.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/module.h>
|
||||
#include <sys/proc.h>
|
||||
#include <sys/lock.h>
|
||||
#include <sys/rwlock.h>
|
||||
#include <sys/mutex.h>
|
||||
#include <sys/systm.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/vmmeter.h>
|
||||
#include <sys/eventhandler.h>
|
||||
|
||||
#include <machine/bus.h>
|
||||
|
||||
#include <vm/vm.h>
|
||||
#include <vm/pmap.h>
|
||||
#include <vm/vm_param.h>
|
||||
#include <vm/vm_object.h>
|
||||
#include <vm/vm_page.h>
|
||||
#include <vm/vm_pager.h>
|
||||
#include <vm/vm_phys.h>
|
||||
|
||||
#define HUGE_PAGE_SIZE (2 * 1024 * 1024) // 2MB Large Page
|
||||
|
||||
MALLOC_DEFINE(M_CONTIGMEM, "contigmem", "contigmem(4) allocations");
|
||||
static void *hugepage_ptr = NULL;
|
||||
|
||||
// struct syscall_hello {
|
||||
// unsigned long size;
|
||||
// };
|
||||
static int load_handler(module_t mod, int event, void *arg) {
|
||||
switch (event) {
|
||||
case MOD_LOAD:
|
||||
printf("Loading Huge Page Kernel Module...\n");
|
||||
|
||||
struct syscall_alloc {
|
||||
int size;
|
||||
int malloc;
|
||||
// int *newSize;
|
||||
// void *addr;
|
||||
};
|
||||
hugepage_ptr = kmem_alloc_contig(HUGE_PAGE_SIZE, M_WAITOK,
|
||||
0, BUS_SPACE_MAXADDR,
|
||||
HUGE_PAGE_SIZE, 0,
|
||||
VM_MEMATTR_DEFAULT);
|
||||
|
||||
// syscall for allocating contigous memory.
|
||||
static int
|
||||
Alloc(struct thread *td, struct syscall_alloc *uap)
|
||||
{
|
||||
if (!hugepage_ptr) {
|
||||
printf("Failed to allocate a huge page!\n");
|
||||
return ENOMEM;
|
||||
}
|
||||
printf("Huge page allocated at %p\n", hugepage_ptr);
|
||||
break;
|
||||
|
||||
// struct syscall_alloc *uap;
|
||||
// uap = (struct syscall_alloc *)arg;
|
||||
case MOD_UNLOAD:
|
||||
if (hugepage_ptr) {
|
||||
kmem_free(hugepage_ptr, HUGE_PAGE_SIZE);
|
||||
printf("Huge page freed.\n");
|
||||
}
|
||||
printf("Unloading module...\n");
|
||||
break;
|
||||
|
||||
struct syscall_alloc state_copy;
|
||||
|
||||
state_copy = *uap;
|
||||
|
||||
int uap_size = 4;
|
||||
// int malloc = uap->malloc;
|
||||
|
||||
printf("size %d \n", state_copy.size);
|
||||
|
||||
// printf("malloc tried 1 %d \n",*uap->malloc);
|
||||
|
||||
if(state_copymalloc == 1) {
|
||||
// Calculate next power of 2
|
||||
|
||||
printf("Malloc called \n");
|
||||
|
||||
int alignment = 1;
|
||||
|
||||
// 2 refers to the size
|
||||
while ( alignment <= uap_size) {
|
||||
alignment = alignment << 1 ;
|
||||
default:
|
||||
return EOPNOTSUPP;
|
||||
}
|
||||
|
||||
// uap->size = 3;
|
||||
|
||||
// td->td_retval[0] = uap;
|
||||
|
||||
|
||||
// printf("Changing size \n");
|
||||
|
||||
state_copy.size = 10;
|
||||
|
||||
*uap = state_copy;
|
||||
|
||||
// *arg = uap;
|
||||
|
||||
printf("alignment %d \n", alignment);
|
||||
|
||||
// To call contig alloc and free based on
|
||||
// hardcoded physical allocations and adding
|
||||
// doing array allocations and frees.
|
||||
// void *addr;
|
||||
|
||||
// int alignmentInt;
|
||||
// // alignmentInt = nextPowerOf2(2);
|
||||
// unsigned long alignment = ( unsigned long ) alignmentInt ;
|
||||
|
||||
// addr = contigmalloc(uap_size, M_CONTIGMEM, M_ZERO,
|
||||
// 0, BUS_SPACE_MAXADDR, alignment, 0);
|
||||
|
||||
// uap->addr = addr;
|
||||
|
||||
// printf("Malloc complete");
|
||||
|
||||
// to write address to /proc and to read allocated address from /proc
|
||||
|
||||
return (4);
|
||||
|
||||
} else {
|
||||
|
||||
// printf("Free called complete");
|
||||
|
||||
// contigfree(uap->addr,uap->size, M_CONTIGMEM);
|
||||
|
||||
// printf("Free complete");
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
return (0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// // syscall for allocating contigous memory.
|
||||
// static int
|
||||
// Free(struct thread *td, void *arg)
|
||||
// {
|
||||
|
||||
// struct syscall_alloc *uap;
|
||||
// uap = (struct syscall_alloc *)arg;
|
||||
|
||||
// unsigned long uap_size = uap->size;
|
||||
|
||||
// printf("free size %lu \n", uap_size);
|
||||
|
||||
|
||||
// // // Calculate next power of 2
|
||||
|
||||
// // unsigned long alignment = 1;
|
||||
|
||||
// // // 2 refers to the size
|
||||
// // while ( alignment <= uap_size) {
|
||||
// // alignment = alignment << 1 ;
|
||||
// // }
|
||||
|
||||
// // printf("alignment %lu \n", alignment);
|
||||
|
||||
// // // To call contig alloc and free based on
|
||||
// // // hardcoded physical allocations and adding
|
||||
// // // doing array allocations and frees.
|
||||
// // void *addr;
|
||||
|
||||
// // int alignmentInt;
|
||||
// // // alignmentInt = nextPowerOf2(2);
|
||||
// // unsigned long alignment = ( unsigned long ) alignmentInt ;
|
||||
|
||||
// contigfree(uap->addr,uap->size, M_CONTIGMEM);
|
||||
|
||||
// // addr = contigmalloc(uap_size, M_CONTIGMEM, M_ZERO,
|
||||
// // 0, BUS_SPACE_MAXADDR, alignment, 0);
|
||||
|
||||
// // int *addr1;
|
||||
// // addr1 = contigmalloc(2, M_CONTIGMEM, M_ZERO,
|
||||
// // 0, BUS_SPACE_MAXADDR, alignment, 0);
|
||||
|
||||
// // uap->addr = addr;
|
||||
|
||||
// // addr1[0] = 2;
|
||||
|
||||
// // printf("address 0 %i \n", addr[0]);
|
||||
// // printf("address 0 %i \n", addr1[0]);
|
||||
|
||||
// // contigfree(addr,2, M_CONTIGMEM);
|
||||
// // contigfree(addr1,2, M_CONTIGMEM);
|
||||
|
||||
// // printf("contigfree complete");
|
||||
|
||||
// // printf("hello kernel 1\n");
|
||||
|
||||
// // printf("hello kernel\n");
|
||||
// return (0);
|
||||
// }
|
||||
// define custom args.
|
||||
/*
|
||||
* The function for implementing the syscall.
|
||||
*/
|
||||
// static int
|
||||
// hello(struct thread *td, void *arg)
|
||||
// {
|
||||
|
||||
// struct syscall_hello *uap;
|
||||
// uap = (struct syscall_hello *)arg;
|
||||
|
||||
// unsigned long uap_size = uap->size;
|
||||
|
||||
// printf("size %lu \n", uap_size);
|
||||
|
||||
|
||||
// // Calculate next power of 2
|
||||
|
||||
// unsigned long alignment = 1;
|
||||
|
||||
// // 2 refers to the size
|
||||
// while ( alignment <= 2) {
|
||||
// alignment = alignment << 1 ;
|
||||
// }
|
||||
|
||||
// printf("alignment %lu \n", alignment);
|
||||
|
||||
// // To call contig alloc and free based on
|
||||
// // hardcoded physical allocations and adding
|
||||
// // doing array allocations and frees.
|
||||
// int *addr;
|
||||
|
||||
// // int alignmentInt;
|
||||
// // // alignmentInt = nextPowerOf2(2);
|
||||
// // unsigned long alignment = ( unsigned long ) alignmentInt ;
|
||||
|
||||
// addr = contigmalloc(2, M_CONTIGMEM, M_ZERO,
|
||||
// 0, BUS_SPACE_MAXADDR, alignment, 0);
|
||||
|
||||
// addr[0] = 1;
|
||||
|
||||
// int *addr1;
|
||||
// addr1 = contigmalloc(2, M_CONTIGMEM, M_ZERO,
|
||||
// 0, BUS_SPACE_MAXADDR, alignment, 0);
|
||||
|
||||
// addr1[0] = 2;
|
||||
|
||||
// printf("address 0 %i \n", addr[0]);
|
||||
// printf("address 0 %i \n", addr1[0]);
|
||||
|
||||
// contigfree(addr,2, M_CONTIGMEM);
|
||||
// contigfree(addr1,2, M_CONTIGMEM);
|
||||
|
||||
// printf("contigfree complete");
|
||||
|
||||
// printf("hello kernel 1\n");
|
||||
|
||||
// printf("hello kernel\n");
|
||||
// return (0);
|
||||
// }
|
||||
|
||||
// calculate next power of 2
|
||||
// int nextPowerOf2 (unsigned int x) {
|
||||
// int value = 1;
|
||||
|
||||
// while ( value <= x) {
|
||||
// value = value << 1 ;
|
||||
// }
|
||||
|
||||
// return value;
|
||||
// }
|
||||
|
||||
/*
|
||||
* The `sysent' for the new syscall
|
||||
*/
|
||||
static struct sysent Malloc_sysent = {
|
||||
.sy_narg = 1,
|
||||
.sy_call = (sy_call_t*)Alloc
|
||||
static moduledata_t superpage_mod = {
|
||||
"superpage",
|
||||
load_handler,
|
||||
NULL
|
||||
};
|
||||
|
||||
// static struct sysent Free_sysent = {
|
||||
// .sy_narg = 0,
|
||||
// .sy_call = Free
|
||||
// };
|
||||
|
||||
/*
|
||||
* The offset in sysent where the syscall is allocated.
|
||||
*/
|
||||
static int Mallocoffset = NO_SYSCALL;
|
||||
// static int Freeoffset = NO_SYSCALL;
|
||||
|
||||
/*
|
||||
* The function called at load/unload.
|
||||
*/
|
||||
static int
|
||||
load(struct module *module, int cmd, void *arg)
|
||||
{
|
||||
int error = 0;
|
||||
|
||||
switch (cmd) {
|
||||
case MOD_LOAD :
|
||||
printf("Malloc syscall loaded at %d\n", Mallocoffset);
|
||||
// printf("Free syscall loaded at %d\n", Freeoffset);
|
||||
break;
|
||||
case MOD_UNLOAD :
|
||||
printf("Malloc syscall unloaded from %d\n", Mallocoffset);
|
||||
// printf("Free syscall unloaded from %d\n", Freeoffset);
|
||||
break;
|
||||
default :
|
||||
error = EOPNOTSUPP;
|
||||
break;
|
||||
}
|
||||
return (error);
|
||||
}
|
||||
|
||||
SYSCALL_MODULE(syscall, &Mallocoffset, &Malloc_sysent, load, NULL);
|
||||
// SYSCALL_MODULE(syscall, &Freeoffset, &Free_sysent, load, NULL);
|
||||
DECLARE_MODULE(superpage, superpage_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#SPDX-License-Identifier: BSD-3-Clause
|
||||
# Copyright(c) 2018 Intel Corporation
|
||||
|
||||
kmods = ['Hugepage', 'nic_uio']
|
||||
kmods = ['Malloc', 'nic_uio']
|
||||
|
||||
# for building kernel modules, we use kernel build system using make, as
|
||||
# with Linux. We have a skeleton BSDmakefile, which pulls many of its
|
||||
|
||||
Reference in New Issue
Block a user