matos/drivers/zero.c

81 lines
1.8 KiB
C
Raw 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-02-07 23:15:56 +01:00
#include "kernel.h"
#include "klibc.h"
2023-11-10 09:01:55 +01:00
struct zeroMappedEntry {
2024-02-07 23:15:56 +01:00
int refCnt;
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) {
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-02-07 23:15:56 +01:00
(void)vreg;
int ret = 0;
paddr_t ppage = allocPhyPage(1);
ret = pageMap(ALIGN_DOWN(addr, PAGE_SIZE), ppage, right | PAGING_MEM_USER) ;
unrefPhyPage(ppage);
if (ret) {
return ret;
}
memset((void *)ALIGN_DOWN(addr, PAGE_SIZE), 0, PAGE_SIZE);
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));
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
}