1 /****************************************************************************
3 * SciTech OS Portability Manager Library
5 * ========================================================================
7 * The contents of this file are subject to the SciTech MGL Public
8 * License Version 1.0 (the "License"); you may not use this file
9 * except in compliance with the License. You may obtain a copy of
10 * the License at http://www.scitechsoft.com/mgl-license.txt
12 * Software distributed under the License is distributed on an
13 * "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * rights and limitations under the License.
17 * The Original Code is Copyright (C) 1991-1998 SciTech Software, Inc.
19 * The Initial Developer of the Original Code is SciTech Software, Inc.
20 * All Rights Reserved.
22 * ========================================================================
27 * Description: Implementation for the OS Portability Manager Library, which
28 * contains functions to implement OS specific services in a
29 * generic, cross platform API. Porting the OS Portability
30 * Manager library is the first step to porting any SciTech
31 * products to a new platform.
33 ****************************************************************************/
36 #include "drvlib/os/os.h"
47 #include "qnx/vbios.h"
49 #include <sys/seginfo.h>
50 #include <sys/console.h>
54 #include <sys/neutrino.h>
55 #include <sys/dcmd_chr.h>
58 /*--------------------------- Global variables ----------------------------*/
60 static uint VESABuf_len = 1024; /* Length of the VESABuf buffer */
61 static void *VESABuf_ptr = NULL; /* Near pointer to VESABuf */
62 static uint VESABuf_rseg; /* Real mode segment of VESABuf */
63 static uint VESABuf_roff; /* Real mode offset of VESABuf */
64 static VBIOSregs_t *VRegs = NULL; /* Pointer to VBIOS registers */
65 static int raw_count = 0;
66 static struct _console_ctrl *cc = NULL;
67 static int console_count = 0;
68 static int rmbuf_inuse = 0;
70 static void (PMAPIP fatalErrorCleanup)(void) = NULL;
72 /*----------------------------- Implementation ----------------------------*/
74 void PMAPI PM_init(void)
80 ThreadCtl(_NTO_TCTL_IO, 0); /* Get IO privilidge */
82 force = getenv("VBIOS_METHOD");
83 VRegs = VBIOSinit(force ? atoi(force) : 0);
90 ibool PMAPI PM_haveBIOSAccess(void)
91 { return VRegs != NULL; }
93 long PMAPI PM_getOSType(void)
96 int PMAPI PM_getModeType(void)
99 void PMAPI PM_backslash(char *s)
101 uint pos = strlen(s);
102 if (s[pos-1] != '/') {
108 void PMAPI PM_setFatalErrorCleanup(
109 void (PMAPIP cleanup)(void))
111 fatalErrorCleanup = cleanup;
114 void PMAPI PM_fatalError(const char *msg)
116 if (fatalErrorCleanup)
118 fprintf(stderr,"%s\n", msg);
122 static void ExitVBEBuf(void)
125 PM_freeRealSeg(VESABuf_ptr);
129 void * PMAPI PM_getVESABuf(uint *len,uint *rseg,uint *roff)
132 /* Allocate a global buffer for communicating with the VESA VBE */
133 if ((VESABuf_ptr = PM_allocRealSeg(VESABuf_len, &VESABuf_rseg, &VESABuf_roff)) == NULL)
138 *rseg = VESABuf_rseg;
139 *roff = VESABuf_roff;
143 static int term_raw(void)
145 struct termios termios_p;
150 /* Go into "raw" input mode */
151 if (tcgetattr(STDIN_FILENO, &termios_p))
154 termios_p.c_cc[VMIN] = 1;
155 termios_p.c_cc[VTIME] = 0;
156 termios_p.c_lflag &= ~( ECHO|ICANON|ISIG|ECHOE|ECHOK|ECHONL);
157 tcsetattr(STDIN_FILENO, TCSADRAIN, &termios_p);
161 static void term_restore(void)
163 struct termios termios_p;
165 if (raw_count-- != 1)
168 tcgetattr(STDIN_FILENO, &termios_p);
169 termios_p.c_lflag |= (ECHO|ICANON|ISIG|ECHOE|ECHOK|ECHONL);
170 termios_p.c_oflag |= (OPOST);
171 tcsetattr(STDIN_FILENO, TCSADRAIN, &termios_p);
174 int PMAPI PM_kbhit(void)
178 if (term_raw() == -1)
181 /* Go into non blocking mode */
182 blocking = fcntl(STDIN_FILENO, F_GETFL) | O_NONBLOCK;
183 fcntl(STDIN_FILENO, F_SETFL, blocking);
186 /* restore blocking mode */
187 fcntl(STDIN_FILENO, F_SETFL, blocking & ~O_NONBLOCK);
197 int PMAPI PM_getch(void)
201 if (term_raw() == -1)
204 #if defined(__QNX__) && !defined(__QNXNTO__)
214 PM_HWND PMAPI PM_openConsole(
227 if ((fd = open("/dev/con1", O_RDWR)) == -1)
229 cc = console_open(fd, O_RDWR);
237 int PMAPI PM_getConsoleStateSize(void)
239 return PM_getVGAStateSize() + sizeof(int) * 3;
242 void PMAPI PM_saveConsoleState(void *stateBuf,int console_id)
248 if ((fd = open("/dev/con1", O_RDWR)) == -1)
250 flags = _CONCTL_INVISIBLE_CHG | _CONCTL_INVISIBLE;
251 devctl(fd, DCMD_CHR_SERCTL, &flags, sizeof flags, 0);
254 uchar *buf = &((uchar*)stateBuf)[PM_getVGAStateSize()];
256 /* Save QNX 4 console state */
257 console_read(cc, -1, 0, NULL, 0,
258 (int *)buf+1, (int *)buf+2, NULL);
259 *(int *)buf = console_ctrl(cc, -1,
260 CONSOLE_NORESIZE | CONSOLE_NOSWITCH | CONSOLE_INVISIBLE,
261 CONSOLE_NORESIZE | CONSOLE_NOSWITCH | CONSOLE_INVISIBLE);
263 /* Save state of VGA registers */
264 PM_saveVGAState(stateBuf);
268 void PMAPI PM_setSuspendAppCallback(int (_ASMAPIP saveState)(int flags))
270 /* TODO: Implement support for console switching if possible */
273 void PMAPI PM_restoreConsoleState(const void *stateBuf,PM_HWND hwndConsole)
279 if ((fd = open("/dev/con1", O_RDWR)) == -1)
281 flags = _CONCTL_INVISIBLE_CHG;
282 devctl(fd, DCMD_CHR_SERCTL, &flags, sizeof flags, 0);
285 uchar *buf = &((uchar*)stateBuf)[PM_getVGAStateSize()];
287 /* Restore the state of the VGA compatible registers */
288 PM_restoreVGAState(stateBuf);
290 /* Restore QNX 4 console state */
291 console_ctrl(cc, -1, *(int *)buf,
292 CONSOLE_NORESIZE | CONSOLE_NOSWITCH | CONSOLE_INVISIBLE);
293 console_write(cc, -1, 0, NULL, 0,
294 (int *)buf+1, (int *)buf+2, NULL);
298 void PMAPI PM_closeConsole(PM_HWND hwndConsole)
301 if (--console_count == 0) {
308 void PM_setOSCursorLocation(int x,int y)
313 console_write(cc, -1, 0, NULL, 0, &y, &x, NULL);
317 void PM_setOSScreenWidth(int width,int height)
321 ibool PMAPI PM_setRealTimeClockHandler(PM_intHandler ih, int frequency)
323 // TODO: Implement this for QNX
327 void PMAPI PM_setRealTimeClockFrequency(int frequency)
329 // TODO: Implement this for QNX
332 void PMAPI PM_restoreRealTimeClockHandler(void)
334 // TODO: Implement this for QNX
337 char * PMAPI PM_getCurrentPath(
341 return getcwd(path,maxLen);
344 char PMAPI PM_getBootDrive(void)
347 const char * PMAPI PM_getVBEAFPath(void)
348 { return PM_getNucleusConfigPath(); }
350 const char * PMAPI PM_getNucleusPath(void)
352 char *env = getenv("NUCLEUS_PATH");
355 return env ? env : "/nto/scitech/x86/bin";
356 #elif defined (__PPC__)
357 return env ? env : "/nto/scitech/ppcbe/bin";
358 #elif defined (__MIPS__)
360 return env ? env : "/nto/scitech/mipsbe/bin";
362 return env ? env : "/nto/scitech/mipsle/bin";
364 #elif defined (__SH__)
366 return env ? env : "/nto/scitech/shbe/bin";
368 return env ? env : "/nto/scitech/shle/bin";
370 #elif defined (__ARM__)
371 return env ? env : "/nto/scitech/armle/bin";
374 return env ? env : "/qnx4/scitech/bin";
378 const char * PMAPI PM_getNucleusConfigPath(void)
380 static char path[512];
384 gethostname(temp, sizeof (temp));
385 temp[sizeof (temp) - 1] = '\0'; /* Paranoid */
386 sprintf(path,"/etc/config/scitech/%s/config", temp);
388 sprintf(path,"/etc/config/scitech/%d/config", getnid());
390 if ((env = getenv("NUCLEUS_PATH")) != NULL) {
393 strcat(path,"config");
398 const char * PMAPI PM_getUniqueID(void)
400 static char buf[128];
402 gethostname(buf, sizeof (buf));
404 sprintf(buf,"node%d", getnid());
409 const char * PMAPI PM_getMachineName(void)
411 static char buf[128];
413 gethostname(buf, sizeof (buf));
415 sprintf(buf,"node%d", getnid());
420 void * PMAPI PM_getBIOSPointer(void)
422 return PM_mapRealPointer(0, 0x400);
425 void * PMAPI PM_getA0000Pointer(void)
427 static void *ptr = NULL;
429 unsigned offset, i, maplen;
434 /* Some trickery is required to get the linear address 64K aligned */
435 for (i = 0; i < 5; i++) {
436 ptr = PM_mapPhysicalAddr(0xA0000,0xFFFF,true);
437 offset = 0x10000 - ((unsigned)ptr % 0x10000);
440 munmap(ptr, 0x10000);
441 maplen = 0x10000 + offset;
442 freeptr = PM_mapPhysicalAddr(0xA0000-offset, maplen-1,true);
443 ptr = (void *)(offset + (unsigned)freeptr);
444 if (0x10000 - ((unsigned)ptr % 0x10000))
446 munmap(freeptr, maplen);
449 printf("Could not get a 64K aligned linear address for A0000 region\n");
455 void * PMAPI PM_mapPhysicalAddr(ulong base,ulong limit,ibool isCached)
459 unsigned prot = PROT_READ|PROT_WRITE|(isCached?0:PROT_NOCACHE);
461 int pagesize = __PAGESIZE;
465 int rounddown = base % pagesize;
467 static int __VidFD = -1;
471 if (base < rounddown)
479 if ((__VidFD = shm_open( "Physical", O_RDWR, 0777 )) == -1) {
480 perror( "Cannot open Physical memory" );
485 limit = (limit + o + 0xFFF) & ~0xFFF;
486 if ((int)(p = mmap( 0, limit, prot, MAP_SHARED,
487 __VidFD, base )) == -1 ) {
492 if ((p = mmap(0, limit, prot, MAP_PHYS | MAP_SHARED,
493 NOFD, base)) == MAP_FAILED) {
497 return (p + rounddown);
500 void PMAPI PM_freePhysicalAddr(void *ptr,ulong limit)
505 ulong PMAPI PM_getPhysicalAddr(void *p)
507 // TODO: This function should find the physical address of a linear
512 ibool PMAPI PM_getPhysicalAddrRange(
517 // TODO: Implement this!
521 void PMAPI PM_sleep(ulong milliseconds)
523 // TODO: Put the process to sleep for milliseconds
526 int PMAPI PM_getCOMPort(int port)
528 // TODO: Re-code this to determine real values using the Plug and Play
529 // manager for the OS.
531 case 0: return 0x3F8;
532 case 1: return 0x2F8;
537 int PMAPI PM_getLPTPort(int port)
539 // TODO: Re-code this to determine real values using the Plug and Play
540 // manager for the OS.
542 case 0: return 0x3BC;
543 case 1: return 0x378;
544 case 2: return 0x278;
549 void * PMAPI PM_mallocShared(long size)
551 return PM_malloc(size);
554 void PMAPI PM_freeShared(void *ptr)
559 void * PMAPI PM_mapToProcess(void *base,ulong limit)
560 { return (void*)base; }
562 void * PMAPI PM_mapRealPointer(uint r_seg,uint r_off)
568 if ((p = VBIOSgetmemptr(r_seg, r_off, VRegs)) == (void *)-1)
573 void * PMAPI PM_allocRealSeg(uint size,uint *r_seg,uint *r_off)
576 printf("PM_allocRealSeg: can't handle %d bytes\n", size);
579 if (rmbuf_inuse != 0) {
580 printf("PM_allocRealSeg: transfer area already in use\n");
585 *r_seg = VBIOS_TransBufVSeg(VRegs);
586 *r_off = VBIOS_TransBufVOff(VRegs);
587 return (void*)VBIOS_TransBufPtr(VRegs);
590 void PMAPI PM_freeRealSeg(void *mem)
592 if (rmbuf_inuse == 0) {
593 printf("PM_freeRealSeg: nothing was allocated\n");
599 void PMAPI DPMI_int86(int intno, DPMI_regs *regs)
605 VRegs->l.eax = regs->eax;
606 VRegs->l.ebx = regs->ebx;
607 VRegs->l.ecx = regs->ecx;
608 VRegs->l.edx = regs->edx;
609 VRegs->l.esi = regs->esi;
610 VRegs->l.edi = regs->edi;
612 VBIOSint(intno, VRegs, 1024);
614 regs->eax = VRegs->l.eax;
615 regs->ebx = VRegs->l.ebx;
616 regs->ecx = VRegs->l.ecx;
617 regs->edx = VRegs->l.edx;
618 regs->esi = VRegs->l.esi;
619 regs->edi = VRegs->l.edi;
620 regs->flags = VRegs->w.flags & 0x1;
623 int PMAPI PM_int86(int intno, RMREGS *in, RMREGS *out)
629 VRegs->l.eax = in->e.eax;
630 VRegs->l.ebx = in->e.ebx;
631 VRegs->l.ecx = in->e.ecx;
632 VRegs->l.edx = in->e.edx;
633 VRegs->l.esi = in->e.esi;
634 VRegs->l.edi = in->e.edi;
636 VBIOSint(intno, VRegs, 1024);
638 out->e.eax = VRegs->l.eax;
639 out->e.ebx = VRegs->l.ebx;
640 out->e.ecx = VRegs->l.ecx;
641 out->e.edx = VRegs->l.edx;
642 out->e.esi = VRegs->l.esi;
643 out->e.edi = VRegs->l.edi;
644 out->x.cflag = VRegs->w.flags & 0x1;
649 int PMAPI PM_int86x(int intno, RMREGS *in, RMREGS *out,
657 time_t today = time(NULL);
659 t = localtime(&today);
660 out->x.cx = t->tm_year + 1900;
661 out->h.dh = t->tm_mon + 1;
662 out->h.dl = t->tm_mday;
666 VRegs->l.eax = in->e.eax;
667 VRegs->l.ebx = in->e.ebx;
668 VRegs->l.ecx = in->e.ecx;
669 VRegs->l.edx = in->e.edx;
670 VRegs->l.esi = in->e.esi;
671 VRegs->l.edi = in->e.edi;
672 VRegs->w.es = sregs->es;
673 VRegs->w.ds = sregs->ds;
675 VBIOSint(intno, VRegs, 1024);
677 out->e.eax = VRegs->l.eax;
678 out->e.ebx = VRegs->l.ebx;
679 out->e.ecx = VRegs->l.ecx;
680 out->e.edx = VRegs->l.edx;
681 out->e.esi = VRegs->l.esi;
682 out->e.edi = VRegs->l.edi;
683 out->x.cflag = VRegs->w.flags & 0x1;
684 sregs->es = VRegs->w.es;
685 sregs->ds = VRegs->w.ds;
691 void PMAPI PM_callRealMode(uint seg,uint off, RMREGS *in,
698 VRegs->l.eax = in->e.eax;
699 VRegs->l.ebx = in->e.ebx;
700 VRegs->l.ecx = in->e.ecx;
701 VRegs->l.edx = in->e.edx;
702 VRegs->l.esi = in->e.esi;
703 VRegs->l.edi = in->e.edi;
704 VRegs->w.es = sregs->es;
705 VRegs->w.ds = sregs->ds;
707 VBIOScall(seg, off, VRegs, 1024);
709 in->e.eax = VRegs->l.eax;
710 in->e.ebx = VRegs->l.ebx;
711 in->e.ecx = VRegs->l.ecx;
712 in->e.edx = VRegs->l.edx;
713 in->e.esi = VRegs->l.esi;
714 in->e.edi = VRegs->l.edi;
715 in->x.cflag = VRegs->w.flags & 0x1;
716 sregs->es = VRegs->w.es;
717 sregs->ds = VRegs->w.ds;
720 void PMAPI PM_availableMemory(ulong *physical,ulong *total)
723 *physical = *total = _memavl();
727 void * PMAPI PM_allocLockedMem(
733 // TODO: Implement this on QNX
737 void PMAPI PM_freeLockedMem(
742 // TODO: Implement this on QNX
745 void * PMAPI PM_allocPage(
748 // TODO: Implement this on QNX
752 void PMAPI PM_freePage(
755 // TODO: Implement this on QNX
758 void PMAPI PM_setBankA(int bank)
764 VRegs->l.eax = 0x4F05;
765 VRegs->l.ebx = 0x0000;
767 VBIOSint(0x10, VRegs, 1024);
770 void PMAPI PM_setBankAB(int bank)
776 VRegs->l.eax = 0x4F05;
777 VRegs->l.ebx = 0x0000;
779 VBIOSint(0x10, VRegs, 1024);
781 VRegs->l.eax = 0x4F05;
782 VRegs->l.ebx = 0x0001;
784 VBIOSint(0x10, VRegs, 1024);
787 void PMAPI PM_setCRTStart(int x,int y,int waitVRT)
793 VRegs->l.eax = 0x4F07;
794 VRegs->l.ebx = waitVRT;
797 VBIOSint(0x10, VRegs, 1024);
800 ibool PMAPI PM_doBIOSPOST(
813 int PMAPI PM_lockDataPages(void *p,uint len,PM_lockHandle *lh)
819 int PMAPI PM_unlockDataPages(void *p,uint len,PM_lockHandle *lh)
825 int PMAPI PM_lockCodePages(void (*p)(),uint len,PM_lockHandle *lh)
831 int PMAPI PM_unlockCodePages(void (*p)(),uint len,PM_lockHandle *lh)
837 PM_MODULE PMAPI PM_loadLibrary(
838 const char *szDLLName)
840 // TODO: Implement this to load shared libraries!
845 void * PMAPI PM_getProcAddress(
847 const char *szProcName)
849 // TODO: Implement this!
855 void PMAPI PM_freeLibrary(
858 // TODO: Implement this!
862 int PMAPI PM_setIOPL(
865 // QNX handles IOPL selection at the program link level.
869 /****************************************************************************
871 base - The starting physical base address of the region
872 size - The size in bytes of the region
873 type - Type to place into the MTRR register
876 Error code describing the result.
879 Function to enable write combining for the specified region of memory.
880 ****************************************************************************/
881 int PMAPI PM_enableWriteCombine(
887 return MTRR_enableWriteCombine(base,size,type);
889 return PM_MTRR_NOT_SUPPORTED;