matos/drivers/zero.c

125 lines
2.8 KiB
C
Raw Permalink Normal View History

2024-02-07 23:15:56 +01:00
#include "zero.h"
2023-11-10 09:01:55 +01:00
#include "alloc.h"
2024-03-06 22:00:09 +01:00
#include "errno.h"
2024-02-07 23:15:56 +01:00
#include "kernel.h"
#include "klibc.h"
2024-02-15 18:40:45 +01:00
#include "mem.h"
2024-03-06 22:00:09 +01:00
#include "list.h"
#include "paging.h"
#include "types.h"
struct zeroMappedPage {
uaddr_t mappedAddr;
paddr_t phyAddr;
struct zeroMappedPage *prev, *next;
};
2023-11-10 09:01:55 +01:00
struct zeroMappedEntry {
2024-02-07 23:15:56 +01:00
int refCnt;
2024-03-06 22:00:09 +01:00
struct zeroMappedPage *listMapped;
2023-11-10 09:01:55 +01:00
};
2024-03-06 22:00:09 +01:00
static int insertMappedPage(struct zeroMappedEntry *entry, uaddr_t vAddr, paddr_t pAddr){
struct zeroMappedPage *info = (struct zeroMappedPage *)zalloc(sizeof(struct zeroMappedPage));
if(!info)
return -ENOMEM;
info->mappedAddr = vAddr;
info->phyAddr = pAddr;
list_add_tail(entry->listMapped, info);
return 0;
}
2023-11-10 09:01:55 +01:00
static int zeroOpen(struct uAddrVirtualReg *vreg)
{
2024-02-07 23:15:56 +01:00
struct zeroMappedEntry *ent = (struct zeroMappedEntry *)vreg->res->customData;
ent->refCnt++;
return 0;
2023-11-10 09:01:55 +01:00
}
2024-02-07 23:15:56 +01:00
2023-11-10 09:01:55 +01:00
static int zeroClose(struct uAddrVirtualReg *vreg)
{
2024-02-07 23:15:56 +01:00
struct zeroMappedEntry *ent = (struct zeroMappedEntry *)vreg->res->customData;
ent->refCnt--;
2024-02-10 22:49:24 +01:00
if (ent->refCnt == 0) {
2024-03-06 22:00:09 +01:00
struct zeroMappedPage *pageInfo;
list_collapse(ent->listMapped, pageInfo){
pageUnmap(pageInfo->mappedAddr);
free(pageInfo);
}
2024-02-10 22:49:24 +01:00
free(vreg->res->customData);
free(vreg->res);
2024-02-10 22:49:24 +01:00
}
2024-02-07 23:15:56 +01:00
return 0;
2023-11-10 09:01:55 +01:00
}
2024-02-07 23:15:56 +01:00
2023-11-10 09:01:55 +01:00
static int zeroNoPage(struct uAddrVirtualReg *vreg, uaddr_t addr, int right)
{
2024-03-06 22:00:09 +01:00
int ret = 0;
paddr_t ppage = allocPhyPage(1);
uaddr_t mappedAddr = ALIGN_DOWN(addr, PAGE_SIZE);
ret = pageMap(mappedAddr, ppage, right | PAGING_MEM_USER);
2024-02-07 23:15:56 +01:00
2024-03-06 22:00:09 +01:00
struct zeroMappedEntry *ent = (struct zeroMappedEntry *)vreg->res->customData;
2024-02-07 23:15:56 +01:00
unrefPhyPage(ppage);
if (ret) {
return ret;
}
2024-03-06 22:00:09 +01:00
ret = insertMappedPage(ent, mappedAddr, ppage);
if (ret) {
pageUnmap(mappedAddr);
return ret;
}
memset((void *)mappedAddr, 0, PAGE_SIZE);
2024-02-07 23:15:56 +01:00
return ret;
2023-11-10 09:01:55 +01:00
}
static struct mappedRessourceOps zeroOps = {
.open = zeroOpen,
.close = zeroClose,
.unmap = NULL,
.nopage = zeroNoPage,
};
int zeroOnMapped(struct uAddrVirtualReg *vreg)
2023-11-10 09:01:55 +01:00
{
(void)vreg;
return 0;
}
2023-11-10 09:01:55 +01:00
int zeroMmap(struct uAddrSpace *as, uaddr_t *uaddr, size_t size, uint32_t rights,
uint32_t flags)
{
2024-02-08 20:54:29 +01:00
int ret = 0;
struct mappedRessource *res =
(struct mappedRessource *)zalloc(sizeof(struct mappedRessource));
struct zeroMappedEntry *cust =
(struct zeroMappedEntry *)zalloc(sizeof(struct zeroMappedEntry));
2024-03-06 22:00:09 +01:00
list_init(cust->listMapped);
res->allowedRight = PAGING_MEM_READ | PAGING_MEM_WRITE | PAGING_MEM_EXEC | PAGING_MEM_USER;
res->ops = &zeroOps;
res->customData = cust;
res->onResMapped = zeroOnMapped;
2024-02-08 20:54:29 +01:00
ret = uAddrSpaceMmap(as, uaddr, size, rights, flags, res, 0);
if (ret) {
free(res);
free(cust);
}
return ret;
2023-11-10 09:01:55 +01:00
}