pm.c

来自「适合KS8695X」· C语言 代码 · 共 892 行 · 第 1/2 页

C
892
字号
	}
    if (i == 5) {
	printf("Could not get a 64K aligned linear address for A0000 region\n");
	exit(1);
	}
    return ptr;
}

void * PMAPI PM_mapPhysicalAddr(ulong base,ulong limit,ibool isCached)
{
    uchar_t *p;
    unsigned o;
    unsigned prot = PROT_READ|PROT_WRITE|(isCached?0:PROT_NOCACHE);
#ifdef __PAGESIZE
    int pagesize = __PAGESIZE;
#else
    int pagesize = 4096;
#endif
    int rounddown = base % pagesize;
#ifndef __QNXNTO__
    static int __VidFD = -1;
#endif

    if (rounddown) {
	if (base < rounddown)
	    return NULL;
	base -= rounddown;
	limit += rounddown;
	}

#ifndef __QNXNTO__
    if (__VidFD < 0) {
	if ((__VidFD = shm_open( "Physical", O_RDWR, 0777 )) == -1) {
	    perror( "Cannot open Physical memory" );
	    exit(1);
	    }
	}
    o = base & 0xFFF;
    limit = (limit + o + 0xFFF) & ~0xFFF;
    if ((int)(p = mmap( 0, limit, prot, MAP_SHARED,
	    __VidFD, base )) == -1 ) {
	return NULL;
	}
    p += o;
#else
    if ((p = mmap(0, limit, prot, MAP_PHYS | MAP_SHARED,
	    NOFD, base)) == MAP_FAILED) {
	return (void *)-1;
	}
#endif
    return (p + rounddown);
}

void PMAPI PM_freePhysicalAddr(void *ptr,ulong limit)
{
    munmap(ptr,limit+1);
}

ulong PMAPI PM_getPhysicalAddr(void *p)
{
    /* TODO: This function should find the physical address of a linear */
    /*       address. */
    return 0xFFFFFFFFUL;
}

ibool PMAPI PM_getPhysicalAddrRange(
    void *p,
    ulong length,
    ulong *physAddress)
{
    /* TODO: Implement this! */
    return false;
}

void PMAPI PM_sleep(ulong milliseconds)
{
    /* TODO: Put the process to sleep for milliseconds */
}

int PMAPI PM_getCOMPort(int port)
{
    /* TODO: Re-code this to determine real values using the Plug and Play */
    /*       manager for the OS. */
    switch (port) {
	case 0: return 0x3F8;
	case 1: return 0x2F8;
	}
    return 0;
}

int PMAPI PM_getLPTPort(int port)
{
    /* TODO: Re-code this to determine real values using the Plug and Play */
    /*       manager for the OS. */
    switch (port) {
	case 0: return 0x3BC;
	case 1: return 0x378;
	case 2: return 0x278;
	}
    return 0;
}

void * PMAPI PM_mallocShared(long size)
{
    return PM_malloc(size);
}

void PMAPI PM_freeShared(void *ptr)
{
    PM_free(ptr);
}

void * PMAPI PM_mapToProcess(void *base,ulong limit)
{ return (void*)base; }

void * PMAPI PM_mapRealPointer(uint r_seg,uint r_off)
{
    void *p;

    PM_init();

    if ((p = VBIOSgetmemptr(r_seg, r_off, VRegs)) == (void *)-1)
	return NULL;
    return p;
}

void * PMAPI PM_allocRealSeg(uint size,uint *r_seg,uint *r_off)
{
    if (size > 1024) {
	printf("PM_allocRealSeg: can't handle %d bytes\n", size);
	return 0;
	}
    if (rmbuf_inuse != 0) {
	printf("PM_allocRealSeg: transfer area already in use\n");
	return 0;
	}
    PM_init();
    rmbuf_inuse = 1;
    *r_seg = VBIOS_TransBufVSeg(VRegs);
    *r_off = VBIOS_TransBufVOff(VRegs);
    return (void*)VBIOS_TransBufPtr(VRegs);
}

void PMAPI PM_freeRealSeg(void *mem)
{
    if (rmbuf_inuse == 0) {
	printf("PM_freeRealSeg: nothing was allocated\n");
	return;
	}
    rmbuf_inuse = 0;
}

void PMAPI DPMI_int86(int intno, DPMI_regs *regs)
{
    PM_init();
    if (VRegs == NULL)
	return;

    VRegs->l.eax = regs->eax;
    VRegs->l.ebx = regs->ebx;
    VRegs->l.ecx = regs->ecx;
    VRegs->l.edx = regs->edx;
    VRegs->l.esi = regs->esi;
    VRegs->l.edi = regs->edi;

    VBIOSint(intno, VRegs, 1024);

    regs->eax = VRegs->l.eax;
    regs->ebx = VRegs->l.ebx;
    regs->ecx = VRegs->l.ecx;
    regs->edx = VRegs->l.edx;
    regs->esi = VRegs->l.esi;
    regs->edi = VRegs->l.edi;
    regs->flags = VRegs->w.flags & 0x1;
}

int PMAPI PM_int86(int intno, RMREGS *in, RMREGS *out)
{
    PM_init();
    if (VRegs == NULL)
	return 0;

    VRegs->l.eax = in->e.eax;
    VRegs->l.ebx = in->e.ebx;
    VRegs->l.ecx = in->e.ecx;
    VRegs->l.edx = in->e.edx;
    VRegs->l.esi = in->e.esi;
    VRegs->l.edi = in->e.edi;

    VBIOSint(intno, VRegs, 1024);

    out->e.eax = VRegs->l.eax;
    out->e.ebx = VRegs->l.ebx;
    out->e.ecx = VRegs->l.ecx;
    out->e.edx = VRegs->l.edx;
    out->e.esi = VRegs->l.esi;
    out->e.edi = VRegs->l.edi;
    out->x.cflag = VRegs->w.flags & 0x1;

    return out->x.ax;
}

int PMAPI PM_int86x(int intno, RMREGS *in, RMREGS *out,
    RMSREGS *sregs)
{
    PM_init();
    if (VRegs == NULL)
	return 0;

    if (intno == 0x21) {
	time_t today = time(NULL);
	struct tm *t;
	t = localtime(&today);
	out->x.cx = t->tm_year + 1900;
	out->h.dh = t->tm_mon + 1;
	out->h.dl = t->tm_mday;
	return 0;
	}
    else {
	VRegs->l.eax = in->e.eax;
	VRegs->l.ebx = in->e.ebx;
	VRegs->l.ecx = in->e.ecx;
	VRegs->l.edx = in->e.edx;
	VRegs->l.esi = in->e.esi;
	VRegs->l.edi = in->e.edi;
	VRegs->w.es = sregs->es;
	VRegs->w.ds = sregs->ds;

	VBIOSint(intno, VRegs, 1024);

	out->e.eax = VRegs->l.eax;
	out->e.ebx = VRegs->l.ebx;
	out->e.ecx = VRegs->l.ecx;
	out->e.edx = VRegs->l.edx;
	out->e.esi = VRegs->l.esi;
	out->e.edi = VRegs->l.edi;
	out->x.cflag = VRegs->w.flags & 0x1;
	sregs->es = VRegs->w.es;
	sregs->ds = VRegs->w.ds;

	return out->x.ax;
	}
}

void PMAPI PM_callRealMode(uint seg,uint off, RMREGS *in,
    RMSREGS *sregs)
{
    PM_init();
    if (VRegs == NULL)
	return;

    VRegs->l.eax = in->e.eax;
    VRegs->l.ebx = in->e.ebx;
    VRegs->l.ecx = in->e.ecx;
    VRegs->l.edx = in->e.edx;
    VRegs->l.esi = in->e.esi;
    VRegs->l.edi = in->e.edi;
    VRegs->w.es = sregs->es;
    VRegs->w.ds = sregs->ds;

    VBIOScall(seg, off, VRegs, 1024);

    in->e.eax = VRegs->l.eax;
    in->e.ebx = VRegs->l.ebx;
    in->e.ecx = VRegs->l.ecx;
    in->e.edx = VRegs->l.edx;
    in->e.esi = VRegs->l.esi;
    in->e.edi = VRegs->l.edi;
    in->x.cflag = VRegs->w.flags & 0x1;
    sregs->es = VRegs->w.es;
    sregs->ds = VRegs->w.ds;
}

void PMAPI PM_availableMemory(ulong *physical,ulong *total)
{
#ifndef __QNXNTO__
    *physical = *total = _memavl();
#endif
}

void * PMAPI PM_allocLockedMem(
    uint size,
    ulong *physAddr,
    ibool contiguous,
    ibool below16M)
{
    /* TODO: Implement this on QNX */
    return NULL;
}

void PMAPI PM_freeLockedMem(
    void *p,
    uint size,
    ibool contiguous)
{
    /* TODO: Implement this on QNX */
}

void * PMAPI PM_allocPage(
    ibool locked)
{
    /* TODO: Implement this on QNX */
    return NULL;
}

void PMAPI PM_freePage(
    void *p)
{
    /* TODO: Implement this on QNX */
}

void PMAPI PM_setBankA(int bank)
{
    PM_init();
    if (VRegs == NULL)
	return;

    VRegs->l.eax = 0x4F05;
    VRegs->l.ebx = 0x0000;
    VRegs->l.edx = bank;
    VBIOSint(0x10, VRegs, 1024);
}

void PMAPI PM_setBankAB(int bank)
{
    PM_init();
    if (VRegs == NULL)
	return;

    VRegs->l.eax = 0x4F05;
    VRegs->l.ebx = 0x0000;
    VRegs->l.edx = bank;
    VBIOSint(0x10, VRegs, 1024);

    VRegs->l.eax = 0x4F05;
    VRegs->l.ebx = 0x0001;
    VRegs->l.edx = bank;
    VBIOSint(0x10, VRegs, 1024);
}

void PMAPI PM_setCRTStart(int x,int y,int waitVRT)
{
    PM_init();
    if (VRegs == NULL)
	return;

    VRegs->l.eax = 0x4F07;
    VRegs->l.ebx = waitVRT;
    VRegs->l.ecx = x;
    VRegs->l.edx = y;
    VBIOSint(0x10, VRegs, 1024);
}

ibool PMAPI PM_doBIOSPOST(
    ushort axVal,
    ulong BIOSPhysAddr,
    void *copyOfBIOS,
    ulong BIOSLen)
{
    (void)axVal;
    (void)BIOSPhysAddr;
    (void)copyOfBIOS;
    (void)BIOSLen;
    return false;
}

int PMAPI PM_lockDataPages(void *p,uint len,PM_lockHandle *lh)
{
    p = p;  len = len;
    return 1;
}

int PMAPI PM_unlockDataPages(void *p,uint len,PM_lockHandle *lh)
{
    p = p;  len = len;
    return 1;
}

int PMAPI PM_lockCodePages(void (*p)(),uint len,PM_lockHandle *lh)
{
    p = p;  len = len;
    return 1;
}

int PMAPI PM_unlockCodePages(void (*p)(),uint len,PM_lockHandle *lh)
{
    p = p;  len = len;
    return 1;
}

PM_MODULE PMAPI PM_loadLibrary(
    const char *szDLLName)
{
    /* TODO: Implement this to load shared libraries! */
    (void)szDLLName;
    return NULL;
}

void * PMAPI PM_getProcAddress(
    PM_MODULE hModule,
    const char *szProcName)
{
    /* TODO: Implement this! */
    (void)hModule;
    (void)szProcName;
    return NULL;
}

void PMAPI PM_freeLibrary(
    PM_MODULE hModule)
{
    /* TODO: Implement this! */
    (void)hModule;
}

int PMAPI PM_setIOPL(
    int level)
{
    /* QNX handles IOPL selection at the program link level. */
    return level;
}

/****************************************************************************
PARAMETERS:
base    - The starting physical base address of the region
size    - The size in bytes of the region
type    - Type to place into the MTRR register

RETURNS:
Error code describing the result.

REMARKS:
Function to enable write combining for the specified region of memory.
****************************************************************************/
int PMAPI PM_enableWriteCombine(
    ulong base,
    ulong size,
    uint type)
{
#ifndef  __QNXNTO__
    return MTRR_enableWriteCombine(base,size,type);
#else
    return PM_MTRR_NOT_SUPPORTED;
#endif
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?