int CAudioDecoder::close() { int ret = 0; SDL_DestroyTexture(bmp); // Free the YUV frame if (pFrame) av_frame_free(&pFrame); if (iobuffer) av_freep(iobuffer); // Close the codec if (pCodecCtx) avcodec_close(pCodecCtx); if (pCodecCtxOrig) avcodec_close(pCodecCtxOrig); // Close the video file if (pFormatCtx) avformat_close_input(&pFormatCtx); finit(); bOpen = false; bStarting = false; Release(); return ret; }
static INT8S fs_init (void) { INT32U retv; retv = finit (); spi_hi_speed (__TRUE); if (retv == 0) { retv = fcheck ("S:"); } else { retv = fformat ("S:"); if (retv == 0) { return 0; } else { return -1; } } return 0; }
static int dkpart_init(struct Device_Conf *cf, const char *pfx, int dkno, int partno, offset_t start, offset_t len, FILE *cont, const char *fstype, const char *mntpt){ struct DiskPart *dkp = alloc(sizeof(struct DiskPart)); // name this if( mntpt ){ snprintf(dkp->name, sizeof(dkp->name), "%s", mntpt); }else{ snprintf(dkp->name, sizeof(dkp->name), "%s%d%c:", pfx, dkno, partno + 'a'); } dkp->flags = cf->flags; dkp->fdev = cont; dkp->part_offset = start * DISK_BLOCK_SIZE; dkp->part_len = len * DISK_BLOCK_SIZE; finit( & dkp->file ); dkp->file.d = (void*)dkp; dkp->file.fs = &dkpart_fs; fmount( & dkp->file, dkp->name, 0); if( fstype ) fmount( & dkp->file, dkp->name, fstype ); bootmsg( "%s unit %d/%d %qd blocks mounted on %s type %s\n", cf->name, dkno, partno, len, dkp->name, fstype ); return 0; }
fint irlrs_set_exist_c( fchar lrsset ) { fint okset , naxis ; fint level , error = 0 ; fchar cbuf ; finit( cbuf , MAXTXTLEN ) ; if ( gds_exist_c( lrsset , &error ) ) { okset = EXISTSET ; } else { okset = NOEXISTSET ; } if ( okset == EXISTSET ) { /* does the set exist */ level = 0 ; /* get ORIGIN keyword */ gdsd_rchar_c( lrsset, tofchar( "INSTRUME" ), &level, cbuf, &error ) ; if ( ( error < 0 ) || ( ircc_bandnr_c( cbuf ) != 5 ) ) return( NOTLRSSET ) ; gdsd_rchar_c( lrsset, tofchar( "CTYPE1" ), &level, cbuf, &error ) ; if ( ( error < 0 ) || ( strncmp( cbuf.a , "LAMBDA", 5 ) != 0 ) ) return( NOTLRSSET ) ; gdsd_rchar_c( lrsset, tofchar( "CTYPE2" ), &level, cbuf, &error ) ; if ( ( error < 0 ) || ( strncmp( cbuf.a , "SDET", 4 ) != 0 ) ) return( NOTLRSSET ) ; gdsd_rint_c( lrsset , tofchar( "NAXIS2" ) , &level , &naxis , &error ) ; if ( ( error < 0 ) || ( naxis != 6 ) ) return( NOTLRSSET ) ; } error = 0 ; /* find number of snips */ gdsd_rint_c( lrsset , tofchar( "NAXIS2" ) , &level , &okset , &error ) ; if ( error < 0 ) okset = 0 ; return( okset ) ; }
static #endif /* !PTS_EXTERNAL */ int pts_alloc(int fflags, struct thread *td, struct file *fp) { int unit, ok, error; struct tty *tp; struct pts_softc *psc; struct proc *p = td->td_proc; struct ucred *cred = td->td_ucred; /* Resource limiting. */ PROC_LOCK(p); error = racct_add(p, RACCT_NPTS, 1); if (error != 0) { PROC_UNLOCK(p); return (EAGAIN); } ok = chgptscnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_NPTS)); if (!ok) { racct_sub(p, RACCT_NPTS, 1); PROC_UNLOCK(p); return (EAGAIN); } PROC_UNLOCK(p); /* Try to allocate a new pts unit number. */ unit = alloc_unr(pts_pool); if (unit < 0) { racct_sub(p, RACCT_NPTS, 1); chgptscnt(cred->cr_ruidinfo, -1, 0); return (EAGAIN); } /* Allocate TTY and softc. */ psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO); cv_init(&psc->pts_inwait, "ptsin"); cv_init(&psc->pts_outwait, "ptsout"); psc->pts_unit = unit; psc->pts_cred = crhold(cred); tp = tty_alloc(&pts_class, psc); knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx); knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx); /* Expose the slave device as well. */ tty_makedev(tp, td->td_ucred, "pts/%u", psc->pts_unit); finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops); return (0); }
int main() { pinit(); finit(); while(1) { //mputs("ip = "); //mputs(itoa(disk[IP])); execute(IP); //mputs(" rstack = "); //puts(itoa(*rsp - (DSIZE-(RSSIZE+STSIZE+1)))); //stat(); } }
static void init_card (void) { U32 retv; while ((retv = finit (NULL)) != 0) { /* Wait until the Card is ready */ if (retv == 1) { printf ("\nSD/MMC Init Failed"); printf ("\nInsert Memory card and press key...\n"); } else { printf ("\nSD/MMC Card is Unformatted"); } } }
/*----------------------------------------------------------------------------- * Initialize a Flash Memory Card *----------------------------------------------------------------------------*/ static void init_filesystem (void) { int32_t retv; retv = finit ("M0:"); if (retv == 0) { retv = fmount ("M0:"); if (retv == 0) { printf ("Drive M0 ready!\n"); } else { printf ("Drive M0 mount failed!\n"); } } else { printf ("Drive M0 initialization failed!\n"); } }
int udsk_init(struct Device_Conf *dev){ int port = dev->port; int c = dev->unit; finit( & udsk[c].file ); udsk[c].no = c; udsk[c].fd = open(DISKFILE, 2); udsk[c].file.d = (void*)& udsk[c]; udsk[c].file.fs = & udsk_fs; bootmsg( "%s %qd bytes @ UNIX %s\n", dev->name, DISKSIZE, DISKFILE); dkpart_learn( dev, "ud", c, &udsk[c].file, DISKSIZE / 512 ); return 0; }
void Init (void) { SystemInit(); // Setup and enable the SysTick timer for 100us. SysTick_Config((SystemCoreClock / 10000)); // 100us // Initialize all enabled drives. finit(NULL); // Init status leds Status_LEDs_Init(); // UART Init UART_CONSOLE_Init(); // Load settings SETTINGS_Load(); // Save FIRMWARE Version to Device Settings file SETTINGS_DATA[FIRMWARE_VERSION] = malloc(strlen(SOFTWARE_VERSION)); strcpy(SETTINGS_DATA[FIRMWARE_VERSION], SOFTWARE_VERSION); SETTINGS_Save(); // Setup MAC Address Setup_MAC_Address(); // Init TcpNet init_TcpNet(); // Setup Settings Setup_Settings(); // Reset button init Reset_Button_Init(); // UDP Init UDP_Socket_Init(); // TCP Users Init TCP_Users_Init(TCP_PORT); // Init LED CUBE LED_CUBE_Init(); UART_CONSOLE_Send_String("Init\n"); }
int pts_alloc_external(int fflags, struct thread *td, struct file *fp, struct cdev *dev, const char *name) { int ok, error; struct tty *tp; struct pts_softc *psc; struct proc *p = td->td_proc; struct ucred *cred = td->td_ucred; /* Resource limiting. */ PROC_LOCK(p); error = racct_add(p, RACCT_NPTS, 1); if (error != 0) { PROC_UNLOCK(p); return (EAGAIN); } ok = chgptscnt(cred->cr_ruidinfo, 1, lim_cur(td, RLIMIT_NPTS)); if (!ok) { racct_sub(p, RACCT_NPTS, 1); PROC_UNLOCK(p); return (EAGAIN); } PROC_UNLOCK(p); /* Allocate TTY and softc. */ psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO); cv_init(&psc->pts_inwait, "ptsin"); cv_init(&psc->pts_outwait, "ptsout"); psc->pts_unit = -1; psc->pts_cdev = dev; psc->pts_cred = crhold(cred); tp = tty_alloc(&pts_class, psc); knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx); knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx); /* Expose the slave device as well. */ tty_makedev(tp, td->td_ucred, "%s", name); finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops); return (0); }
int sys_socket(struct thread *td, struct socket_args *uap) { struct socket *so; struct file *fp; int fd, error, type, oflag, fflag; AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol); type = uap->type; oflag = 0; fflag = 0; if ((type & SOCK_CLOEXEC) != 0) { type &= ~SOCK_CLOEXEC; oflag |= O_CLOEXEC; } if ((type & SOCK_NONBLOCK) != 0) { type &= ~SOCK_NONBLOCK; fflag |= FNONBLOCK; } #ifdef MAC error = mac_socket_check_create(td->td_ucred, uap->domain, type, uap->protocol); if (error != 0) return (error); #endif error = falloc(td, &fp, &fd, oflag); if (error != 0) return (error); /* An extra reference on `fp' has been held for us by falloc(). */ error = socreate(uap->domain, &so, type, uap->protocol, td->td_ucred, td); if (error != 0) { fdclose(td, fp, fd); } else { finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops); if ((fflag & FNONBLOCK) != 0) (void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td); td->td_retval[0] = fd; } fdrop(fp, td); return (error); }
FILE * pcterm_init(struct Device_Conf *dev){ finit( & term.file ); term.file.fs = &term_fs; term.file.d = (void*)&term; term.file.codepage = CODEPAGE_PC437; term.scr.x = term.scr.y = 0; if( dev->addr ) term.scr.d = (u_short*)(dev->addr - k_paddr); else term.scr.d = (u_short*)(0xB8000 - k_paddr); if( dev->flags ) term.scr.attr = dev->flags; else term.scr.attr = 7; if( dev->port ) term.kbd.port = dev->port; else term.kbd.port = KBD_PORT; term.kbd.head = term.kbd.tail = term.kbd.len = 0; term.kbd.state = 0; if( install_interrupt(dev->irq, IPL_TTY, kbdirq, dev->name) ){ PANIC("could not install kbd irq"); } #ifndef PLATFORM_EMUL /* config h/w */ clear_screen( &term.file ); move_cursor( &term.file, 0, 0 ); #endif pcterm_port = &term.file; kprintf("%s scrn at mem 0x%x (0x%x) kbd at io 0x%x irq %d\n", dev->name, term.scr.d, (int)term.scr.d + (int)k_paddr, term.kbd.port, dev->irq); return pcterm_port; }
u8 open_font(unsigned int idx) { char font[20]; close_font(); if (! idx) { cur_str.font.idx = 0; return 1; } sprintf(font, "media/%s.fon", FontNames[idx-1]); finit(&FontFAT, "media"); cur_str.font.fh = fopen2(&FontFAT, font, "rb"); if (! cur_str.font.fh) { printf("Couldn't open font file: %s\n", font); return 0; } setbuf(cur_str.font.fh, 0); if(fread(&cur_str.font.height, 1, 1, cur_str.font.fh) != 1) { printf("Failed to read height from font\n"); fclose(cur_str.font.fh); cur_str.font.fh = NULL; return 0; } cur_str.font.idx = idx; idx = 0; u8 *f = (u8 *)font; while(1) { if (fread(f, 4, 1, cur_str.font.fh) != 1) { printf("Failed to parse font range table\n"); fclose(cur_str.font.fh); cur_str.font.fh = NULL; return 0; } u16 start_c = f[0] | (f[1] << 8); u16 end_c = f[2] | (f[3] << 8); cur_str.font.range[idx++] = start_c; cur_str.font.range[idx++] = end_c; if (start_c == 0 && end_c == 0) break; } return 1; }
/* Irds_set_compare checks whether an irds and an lrs set do exist, and if so checks whether they are compatible. */ static fint irds_set_compare( fchar irds , fchar lrsset , fint *irds_exists , fint *set_exists ) { fint level ; fint error = 0 ; double ival = 0 , lval = 0 ; fchar origin ; finit( origin , MAXTXTLEN ) ; switch ( irds_exist_c( irds , &error ) ) { /* does the irds exist ? */ case 0 : *irds_exists = true ; /* yes the irds does exist */ break ; case -1 : *irds_exists = false ; /* no, the irds doesn't exist */ break ; default : *irds_exists = false ; /* yes, but is not an irds */ return( BADIRDSIN ) ; /* return immediately */ break ; } *set_exists = 0 ; switch ( irlrs_set_exist_c( lrsset ) ) { case NOTLRSSET : return( NOTLRSSET ) ; break ; case NOEXISTSET : break ; case EXISTSET : *set_exists = true ; break ; default : break ; } if ( *set_exists && *irds_exists ) { /* compare some keywords */ gdsd_rdble_c( irds , tofchar( "LONCENTR" ), &level, &ival, &error ) ; gdsd_rdble_c( lrsset, tofchar( "LONCENTR" ), &level, &lval, &error ) ; if ( ( error != level ) || ( ival != lval ) ) return( OUTISNOTIN ) ; gdsd_rdble_c( irds , tofchar( "LATCENTR" ), &level, &ival, &error ) ; gdsd_rdble_c( lrsset, tofchar( "LATCENTR" ), &level, &lval, &error ) ; if ( ( error != level ) || ( ival != lval ) ) return( OUTISNOTIN ) ; } return( OK ) ; /* OK -> return */ }
int read_file( char* fn, char *buf, int max_buf_size) { #if _d_sd FILE *f; if (finit() != 0) return; f = fopen ( fn , "r" ); if( f == NULL ) return 0; fread (buf,1,max_buf_size,f); fclose (f); return 1; #elif _d_uffs int f; f = uffs_open ( fn , UO_RDONLY ); if( f == -1 ) return 0; uffs_read (f,buf,max_buf_size); uffs_close (f); return 1; #endif }
int romdisk_init(struct Device_Conf *dev){ int port = dev->port; char info[32]; int c = dev->unit; finit( & romdsk[c].file ); romdsk[c].file.d = (void*)& romdsk[c]; romdsk[c].file.fs = & romdsk_fs; bootmsg( "%s %d bytes\n", dev->name, ROMDISK_SIZE); // set flags=1 to not automount if( ! dev->flags ){ snprintf(info, sizeof(info), "%s:", dev->name); fmount( & romdsk[c].file, info, "fatfs" ); bootmsg( "%s mounted on %s type %s\n", dev->name, info, "fatfs" ); } return (int)& romdsk[c].file; }
/*---------------------------------------------------------------------------- * Initialize a Flash Memory Card *---------------------------------------------------------------------------*/ static void init_card (void) { U32 retv; MMCFG SDCardCfg; char outBuf[26]; GLCD_SetBackColor (White); GLCD_SetTextColor (Blue); GLCD_ClearLn (5); GLCD_ClearLn (6); GLCD_ClearLn (7); GLCD_ClearLn (8); while ((retv = finit ()) != 0) { /* Wait until the Card is ready*/ if (retv == 1) { printf ("\nSD/MMC Init Failed"); printf ("\nInsert Memory card and press key...\n"); getkey (); } else { printf ("\nSD/MMC Card is Unformatted"); strcpy (&in_line[0], "KEIL\r\n"); cmd_format (&in_line[0]); } } mmc_read_config (&SDCardCfg); outBuf[0] = 0; sprintf (&outBuf[0], " SerNr: 0x%08X", SDCardCfg.sernum); GLCD_DisplayString (5, 0, (unsigned char *)outBuf); sprintf (&outBuf[0], " BlockNr:0x%08X", SDCardCfg.blocknr); GLCD_DisplayString (6, 0, (unsigned char *)outBuf); sprintf (&outBuf[0], " RdLen: 0x%04X", SDCardCfg.read_blen); GLCD_DisplayString (7, 0, (unsigned char *)outBuf); sprintf(&outBuf[0], " WrLen: 0x%04X", SDCardCfg.write_blen); GLCD_DisplayString (8, 0, (unsigned char *)outBuf); }
/* initialize the serial ports */ int serial_init(struct Device_Conf *dev){ int i = dev->unit; USART_TypeDef *addr; int irq; int b; int altpins = dev->flags & CFFLAGS_ALTPINS; finit( & com[i].file ); com[i].file.fs = &serial_port_fs; com[i].file.codepage = CODEPAGE_UTF8; com[i].head = com[i].tail = com[i].len = 0; com[i].status = 0; com[i].portno = i; com[i].file.d = (void*)&com[i]; // enable gpio clock, usart clock, configure pins serial_pins_init( i, altpins ); switch(i){ case 0: addr = USART1; com[i].baudclock = apb2_clock_freq(); irq = (int) IRQ_USART1; break; case 1: addr = USART2; com[i].baudclock = apb1_clock_freq(); irq = (int) IRQ_USART2; break; case 2: addr = USART3; com[i].baudclock = apb1_clock_freq(); irq = (int) IRQ_USART3_4; break; default: PANIC("invalid serial"); break; } com[i].addr = addr; if( dev->baud ) serial_setbaud(i, b=dev->baud); else serial_setbaud(i, b=9600); addr->CR1 = 0x0C // enable rx/tx, no parity, 8 bit, ... | 0x20 // enable RX irq | 1; // enable // enable ints nvic_enable( irq, IPL_TTY ); if( !serial0_port ){ serial0_port = &com[i].file; } bootmsg("%s at io 0x%x irq %d %d baud\n", dev->name, addr, irq, b); return (int) &com[i].file; }
/* * Initialise the FPU for this machine. */ BOOT_CODE bool_t Arch_initFpu(void) { /* Enable FPU / SSE / SSE2 / SSE3 / SSSE3 / SSE4 Extensions. */ write_cr4(read_cr4() | CR4_OSFXSR); /* Enable the FPU in general. */ write_cr0((read_cr0() & ~CR0_EMULATION) | CR0_MONITOR_COPROC | CR0_NUMERIC_ERROR); enableFpu(); /* Initialize the fpu state */ finit(); if (config_set(CONFIG_XSAVE)) { uint64_t xsave_features; uint32_t xsave_instruction; uint64_t desired_features = config_default(CONFIG_XSAVE_FEATURE_SET, 1); /* check for XSAVE support */ if (!(x86_cpuid_ecx(1, 0) & BIT(26))) { printf("XSAVE not supported\n"); return false; } /* enable XSAVE support */ write_cr4(read_cr4() | CR4_OSXSAVE); /* check feature mask */ xsave_features = ((uint64_t)x86_cpuid_edx(0x0d, 0x0) << 32) | x86_cpuid_eax(0x0d, 0x0); if ((xsave_features & desired_features) != desired_features) { printf("Requested feature mask is 0x%llx, but only 0x%llx supported\n", desired_features, (long long)xsave_features); return false; } /* enable feature mask */ write_xcr0(desired_features); /* validate the xsave buffer size and instruction */ if (x86_cpuid_ebx(0x0d, 0x0) != CONFIG_XSAVE_SIZE) { printf("XSAVE buffer set set to %d, but should be %d\n", CONFIG_XSAVE_SIZE, x86_cpuid_ecx(0x0d, 0x0)); return false; } /* check if a specialized XSAVE instruction was requested */ xsave_instruction = x86_cpuid_eax(0x0d, 0x1); if (config_set(CONFIG_XSAVE_XSAVEOPT)) { if (!(xsave_instruction & BIT(0))) { printf("XSAVEOPT requested, but not supported\n"); return false; } } else if (config_set(CONFIG_XSAVE_XSAVEC)) { if (!(xsave_instruction & BIT(1))) { printf("XSAVEC requested, but not supported\n"); return false; } } else if (config_set(CONFIG_XSAVE_XSAVES)) { if (!(xsave_instruction & BIT(3))) { printf("XSAVES requested, but not supported\n"); return false; } /* initialize the XSS MSR */ x86_wrmsr(IA32_XSS_MSR, desired_features); } /* Load a NULL fpu state so that the idle thread ends up * with a sensible FPU state and we can optimize our * switch of it */ memzero(&x86KSnullFpuState, sizeof(x86KSnullFpuState)); loadFpuState(&x86KSnullFpuState); } else { /* Store the null fpu state */ saveFpuState(&x86KSnullFpuState); } /* Set the FPU to lazy switch mode */ disableFpu(); return true; }
docommand() { register char *p; register int i; register ADDR addr, bkaddr; struct proct *procp; char s[4]; cntval = 1; adrflg = 0; errflg = 0; if (scallf) { doscall(); setcur(1); lastcom = NOCOM; return; } if (reflag) { /* search for regular expression */ dore(); lastcom = PRCOM; return; } if (cmd == '\0') { if (integ != 0 && var[0] != '\0') { error("Invalid command (1)"); return; } if (integ != 0) { /* print line number */ ffind(integ); fprint(); lastcom = PRCOM; return; } if (var[0] != 0) { printf("Unexpected null command\n"); return; } } switch (cmd) { case 'Y': debug = !debug; break; case 'V': version(); break; case 'M': if (args[0]) { setmap(args); } else { printmap("? map", &txtmap); printmap("/ map", &datmap); } break; case 'x': printregs(); break; case 'X': printpc(); break; case 'a': if (integ) { cpstr(args, "l\n"); } else if (proc[0]) { cpall(args, "T\n"); } else { error("Bad arguments"); break; } goto setbrk; break; case 'l': setcur(1); lastcom = NOCOM; break; case 'T': prfrx(1); lastcom = NOCOM; break; case 't': prframe(); lastcom = NOCOM; break; case 'e': p = args; if (*p == '\0') { #ifndef FLEXNAMES printf("%.16s() in \"%s\"\n", curproc()->pname, curfile); #else printf("%s() in \"%s\"\n", curproc()->pname, curfile); #endif break; } while (*p != '\0') if (*p++ == '.') goto l1; /* argument is procedure name */ procp = findproc(args); if ((procp->pname[0] != '\0') && (procp->sfptr != badfile)) { finit(adrtofilep(procp->paddr)->sfilename); ffind(procp->lineno); } else printf("Can't find %s\n", args); #ifndef FLEXNAMES printf("%.16s() in \"%s\"\n", curproc()->pname, curfile); #else printf("%s() in \"%s\"\n", curproc()->pname, curfile); #endif lastcom = PRCOM; break; l1: /* argument is filename */ finit(args); printf("\"%s\"\n", curfile); lastcom = PRCOM; break; case 'p': if (integ) ffind(integ); fprint(); lastcom = PRCOM; break; case 'q': exit(0); case 'w': if (integ) ffind(integ); i = fline; fback(WINDOW/2); fprintn(WINDOW); ffind(i); lastcom = PRCOM; break; case 'Q': prdebug(); break; case 'z': if (integ) ffind(integ); fprintn(WINDOW); lastcom = PRCOM; break; case '-': fback(integ ? integ : 1); fpargs(); lastcom = PRCOM; break; case '+': fforward(integ ? integ : 1); fpargs(); lastcom = PRCOM; break; case '\n': switch (lastcom) { case PRCOM: fforward(1); fprint(); break; case DSCOM: oaddr += oincr ? oincr : typetosize(otype, WORDSIZE); printf("0x%x/ ", oaddr); dispf((ADDR) oaddr, odesc, oclass == N_RSYM ? oclass : N_GSYM, otype, 0, 0, DSP); break; case DSICOM: dot += oincr; prisploc(); dispi(dot, odesc, N_GSYM, 0, 0); break; } break; case '\004': if (!isatty(0)) exit(0); switch (lastcom) { case PRCOM: fforward(1); printf("\b"); fprintn(WINDOW); lastcom = PRCOM; break; case DSICOM: printf("\b"); for (i=0; i<WINDOW; i++) { dot += oincr; prisploc(); if (dispi(dot, odesc, N_GSYM, 0, 0) == -1) break; } break; case DSCOM: printf("\b"); for (i=0; i<WINDOW; i++) { oaddr += oincr ? oincr : typetosize(otype, WORDSIZE); printf("0x%x/ ", oaddr); if (dispf((ADDR) oaddr, odesc, oclass == N_RSYM ? oclass : N_GSYM, otype, 0, 0, DSP) == -1) break; } break; default: printf("\n"); } break; case 'r': if (args[0] == '\0') getargs(); case 'R': signo = 0; cpstr(oldargs, args); if (debug) error("calling dopcs"); if (integ) cntval = integ; if (!executing) { executing = TRUE; if (integ) cntval = integ; dopcs('r'); executing = FALSE; } if (debug) error("exiting dopcs"); bkaddr = -1; goto f1; case 'c': signo = 0; case 'C': if (proc[0] != '\0' || integ != 0) { setdot(); if (dot == -1) { error("Cannot set temporary breakpoint"); break; } dopcs('b'); bkaddr = dot; } else bkaddr = -1; integ = atoi(args); f1: if (debug) error("calling dopcs"); if (integ) cntval = integ; dopcs('c'); if (debug) error("exiting dopcs"); if (bkaddr != -1) { ADDR dotsave; dotsave = dot; dot = bkaddr; dopcs('d'); dot = dotsave; } if (!signo) printf("Breakpoint"); printf(" at\n"); setcur(1); lastcom = NOCOM; break; case 'S': case 's': signo = 0; integ = atoi(args); singstep(integ ? integ : 1, cmd); if (signo) printf("\n"); setcur(1); lastcom = NOCOM; break; case 'g': if (pid == 0 || signo) { error("Not stopped at breakpoint"); break; } setdot(); if (dot == -1) { error("Bad address"); break; } adrflg = 1; integ = atoi(args); if (integ) cntval = integ; dopcs('c'); if (!signo) printf("Breakpoint"); printf(" at\n"); setcur(1); lastcom = NOCOM; break; case 'k': if (scallx) { userpc = dot = *(ADDR *)(((ADDR)&u)+PC) = pcs; *(ADDR *)(((ADDR)&u)+FP) = fps; *(ADDR *)(((ADDR)&u)+AP) = aps; if (bkpts) bkpts->flag = flagss; scallx = 0; error("Procedure killed"); longjmp(env, 0); } else { dopcs('k'); printf("\n"); lastcom = NOCOM; break; } case 'B': prbkpt(); break; case 'b': setbrk: if (proc[0] == '\0' && integ == 0) { integ = fline; } setdot(); if (dot == -1 || dot == 0) { error("Cannot set breakpoint"); break; } dopcs('b'); s[0] = ' '; s[1] = cmd; s[2] = '\n'; s[3] = 0; s[1] = cmd; printbkpt(s, adrtoprocp(dot), dot); break; case 'd': if (proc[0] == '\0' && integ == 0) { idbkpt(); break; } setdot(); if (dot == -1) { error("Non existent breakpoint"); break; } dopcs('d'); break; case 'D': dabkpt(); error("All breakpoints deleted"); break; case 'm': addr = varaddr(proc[0] ? proc : curproc()->pname, var); printf("stopped with value %d\n", monex(addr, 'd')); setcur(1); lastcom = NOCOM; break; case '?': if (!(var[0] == '.' && var[1] == '\0')) setdot(); if (errflg) { error(errflg); break; } prisploc(); dispi(dot, args[0] ? args : "i", N_GSYM, 0, 0); lastcom = DSICOM; break; case '/': if (var[0] == '.' && var[1] == '\0') { if (integ == 0) integ = oaddr; dispf((ADDR) integ, args[0] ? args : odesc, oclass == N_RSYM ? oclass : N_GSYM, otype, 0, 0, DSP); oaddr = integ; } else if (integ && (var[0] == '\0')) { dispf((ADDR) integ, args, N_GSYM, 0, 0, 0, DSP); oaddr = integ; cpstr(odesc, args); oclass = N_GSYM; otype = 0; } else dispvar(proc, var, args); lastcom = DSCOM; break; case '=': if (var[0] == '\0') { if (proc[0]) { addr = getaddr(proc, integ); if (addr == -1) { error("Unknown address"); break; } } else addr = integ; dispf(addr, args[0] ? args : "x", 0, -1, 0, 0, DSP); } else findvar(proc, var, args[0] ? args : "x", 2); break; case '!': if (var[0] == '\0') addr = getaddr(proc, integ); else addr = varaddr(proc, var); if (addr == -1) error("Unknown variable"); else { if (number(args[0]) || eqany(args[0], ".-")) { char *p; double atof(); union { struct{ int w1, w2; } ww; double d; } dbl; p = (args[0] == '-') ? args+1 : args; for (; *p != '.' && *p != 'e'; p++) { if (!number(*p)) goto l2; } dbl.d = atof(args); putval(addr, 'd', dbl.ww.w1); if (typetodesc(sl_type,0)[0] == 'g') putval(addr+WORDSIZE, 'd', dbl.ww.w2); break; } l2: if (percentflag) *(ADDR *)(((ADDR)&u)+addr) = argvalue(args); else if (sl_class == N_RSYM && addr < 16) putreg(addr,typetodesc(sl_type,subflag)[0], argvalue(args)); else putval(addr,typetodesc(sl_type,subflag)[0], argvalue(args)); } lastcom = NOCOM; break; case '"': printf(args); break; } }
void initplot( void ) /*------------------------------------------------------------------------------ * Description: Initialize plot software. Set viewport and output dimensions. * If output device is a printer, ask user for line width. *------------------------------------------------------------------------------ */ { static fint Funit; /* Ignored by 'pgbeg', use 0 */ static fchar Ffile; /* Device specification */ static fint Fnxysub[2]; /* Number of subdivisions */ static float width; /* Width of output on paper */ static float aspect; /* Aspect ratio of output on paper */ static float uservals[2]; /* Array version of above */ static fint Fnumitems; /* Use in userxxx routines */ static fint Fdfault; /* Use in userxxx routines */ static fint Fr1; /* Return value or level */ static fint len; /* Length of a string */ static fint Flinewidth; /* Width of lines on output device */ static fchar devtype; /* Device specified in 'pgbeg' */ static fint Ferrlev; static fint Foff; Funit = 0; /* Ignored by 'pgbeg' */ fmake( Ffile, 10 ); Ffile = tofchar( "?" ); /* 'pgbeg' will prompt the user to supply a string. */ Fnxysub[0] = 1; /* Default no subdivisions in plot */ Fnxysub[1] = 1; Fnumitems = 2; Fdfault = HIDDEN; Fr1 = userint_c( Fnxysub, &Fnumitems, &Fdfault, KEY_SUBDIV, MES_SUBDIV ); /* Set window and viewport */ Fr1 = pgbeg_c( &Funit, Ffile, &Fnxysub[0], &Fnxysub[1] ); if (Fr1 != 1) { Ferrlev = 4; error_c( &Ferrlev, tofchar("Cannot open output device") ); } /* No NEXTPAGE= keyword */ Foff = tobool( 0 ); (void) pgask_c( &Foff ); /* Change size of the view surface to a specified width */ /* and aspect ratio (=height/width) */ Fnumitems = 2; Fdfault = HIDDEN; uservals[0] = 0.0; uservals[1] = 1.0; Fr1 = userreal_c( uservals, &Fnumitems, &Fdfault, tofchar("PAPER="), tofchar("Give width(cm), aspect ratio: [0.0,1.0]") ); if (Fr1 > 0) { /* If width = 0.0 then the program will select the largest */ /* view surface */ width = uservals[0]; /* Convert from cm to inches */ width /= 2.54; aspect = uservals[1]; (void) pgpap_c( &width, &aspect ); } /* Get device-type code name of the current PGPLOT device */ /* If the destination is a printer (=every destination */ /* except the Tektronix device), use thick lines in the plot */ len = 20; finit(devtype, len); (void) pgqinf_c( tofchar("TYPE"), devtype, &len ); if (strncmp(devtype.a, "TEK4010", 6) == 0) { /* It is a Tektronix */ } else { Fnumitems = 1; Fdfault = HIDDEN; do { Flinewidth = 2; Fr1 = userint_c( &Flinewidth, &Fnumitems, &Fdfault, tofchar("LINEWIDTH="), tofchar("Give line width (1-21): [2]") ); agreed = ((Flinewidth >= 1) && (Flinewidth <= 21)); if (!agreed) { (void) reject_c( tofchar("LINEWIDTH="), tofchar("Invalid number") ); } } while (!agreed); (void) pgslw_c( &Flinewidth ); } { /* Set viewport */ static float Xl, Xr, Yb, Yt; Xl = 0.2; Xr = 0.9; Yb = 0.1; Yt = 0.9; (void) pgsvp_c(&Xl, &Xr, &Yb, &Yt); } }
/* * Initialise the FPU for this machine. */ BOOT_CODE bool_t Arch_initFpu(void) { /* Enable FPU / SSE / SSE2 / SSE3 / SSSE3 / SSE4 Extensions. */ write_cr4(read_cr4() | CR4_OSFXSR); /* Enable the FPU in general. */ write_cr0((read_cr0() & ~CR0_EMULATION) | CR0_MONITOR_COPROC | CR0_NUMERIC_ERROR); enableFpu(); /* Initialize the fpu state */ finit(); if (config_set(CONFIG_XSAVE)) { uint64_t xsave_features; uint32_t xsave_instruction; uint64_t desired_features = config_ternary(CONFIG_XSAVE, CONFIG_XSAVE_FEATURE_SET, 1); xsave_state_t *nullFpuState = (xsave_state_t *) &x86KSnullFpuState; /* create NULL state for FPU to be used by XSAVE variants */ memzero(&x86KSnullFpuState, sizeof(x86KSnullFpuState)); /* check for XSAVE support */ if (!(x86_cpuid_ecx(1, 0) & BIT(26))) { printf("XSAVE not supported\n"); return false; } /* enable XSAVE support */ write_cr4(read_cr4() | CR4_OSXSAVE); /* check feature mask */ xsave_features = ((uint64_t)x86_cpuid_edx(0x0d, 0x0) << 32) | x86_cpuid_eax(0x0d, 0x0); if ((xsave_features & desired_features) != desired_features) { printf("Requested feature mask is 0x%llx, but only 0x%llx supported\n", desired_features, (long long)xsave_features); return false; } /* enable feature mask */ write_xcr0(desired_features); /* validate the xsave buffer size and instruction */ if (x86_cpuid_ebx(0x0d, 0x0) > CONFIG_XSAVE_SIZE) { printf("XSAVE buffer set set to %d, but needs to be at least %d\n", CONFIG_XSAVE_SIZE, x86_cpuid_ebx(0x0d, 0x0)); return false; } if (x86_cpuid_ebx(0x0d, 0x0) < CONFIG_XSAVE_SIZE) { printf("XSAVE buffer set set to %d, but only needs to be %d.\n" "Warning: Memory may be wasted with larger than needed TCBs.\n", CONFIG_XSAVE_SIZE, x86_cpuid_ebx(0x0d, 0x0)); } /* check if a specialized XSAVE instruction was requested */ xsave_instruction = x86_cpuid_eax(0x0d, 0x1); if (config_set(CONFIG_XSAVE_XSAVEOPT)) { if (!(xsave_instruction & BIT(0))) { printf("XSAVEOPT requested, but not supported\n"); return false; } } else if (config_set(CONFIG_XSAVE_XSAVEC)) { if (!(xsave_instruction & BIT(1))) { printf("XSAVEC requested, but not supported\n"); return false; } } else if (config_set(CONFIG_XSAVE_XSAVES)) { if (!(xsave_instruction & BIT(3))) { printf("XSAVES requested, but not supported\n"); return false; } /* AVX state from extended region should be in compacted format */ nullFpuState->header.xcomp_bv = XCOMP_BV_COMPACTED_FORMAT; /* initialize the XSS MSR */ x86_wrmsr(IA32_XSS_MSR, desired_features); } /* copy i387 FPU initial state from FPU */ saveFpuState(&x86KSnullFpuState); nullFpuState->i387.mxcsr = MXCSR_INIT_VALUE; } else { /* Store the null fpu state */ saveFpuState(&x86KSnullFpuState); } /* Set the FPU to lazy switch mode */ disableFpu(); return true; }
void PROTOCOL_Load(int no_dlg) { (void)no_dlg; #ifdef ENABLE_MODULAR FATFS ModuleFAT; FILE *fh; if(! PROTOCOL_HasModule(Model.protocol)) { *loaded_protocol = 0; return; } if(*loaded_protocol == Model.protocol) return; char file[25]; strcpy(file, "protocol/"); if (Model.protocol > PROTOCOL_COUNT) { *loaded_protocol = 0; return; } else { strcat(file, Protocols[Model.protocol].name); } file[17] = '\0'; //truncate filename to 8 characters strcat(file, ".mod"); memset(&ModuleFAT, 0, sizeof(ModuleFAT)); finit(&ModuleFAT, "protocol"); fh = fopen2(&ModuleFAT, file, "r"); //printf("Loading %s: %08lx\n", file, fh); if(! fh) { if(! no_dlg) { sprintf(tempstring, "Misisng protocol:\n%s", file); PAGE_ShowWarning(NULL, tempstring); } return; } setbuf(fh, 0); fread(loaded_protocol, 1, 4 * 1024, fh); fclose(fh); if ((unsigned long)&_data_loadaddr != *loaded_protocol) { if(! no_dlg) { sprintf(tempstring, "Protocol Mismatch:\n%08x\n%08x", (unsigned long)&_data_loadaddr, *loaded_protocol); PAGE_ShowWarning(NULL, tempstring); } *loaded_protocol = 0; return; } //printf("Updated %d (%d) bytes: Data: %08lx %08lx %08lx\n", size, len, *loaded_protocol, *(loaded_protocol+1), *(loaded_protocol+2)); //We use the same file for multiple protocols, so we need to manually set this here *loaded_protocol = Model.protocol; #else if(! PROTOCOL_HasModule(Model.protocol)) { PROTO_Cmds = NULL; printf("Module is not defined!\n"); return; } PROTO_Cmds = Protocols[Model.protocol].cmd; #endif PROTOCOL_SetSwitch(get_module(Model.protocol)); if (PROTOCOL_GetTelemetryState() != PROTO_TELEM_UNSUPPORTED) { memset(&Telemetry, 0, sizeof(Telemetry)); TELEMETRY_SetType(PROTOCOL_GetTelemetryType()); } CurrentProtocolChannelMap = PROTOCOL_GetChannelMap(); }
/* * We only need open() and close() routines. open() calls socreate() * to allocate a "real" object behind the stream and mallocs some state * info for use by the svr4 emulator; close() deallocates the state * information and passes the underlying object to the normal socket close * routine. */ static int streamsopen(struct cdev *dev, int oflags, int devtype, struct thread *td) { struct filedesc *fdp; struct svr4_strm *st; struct socket *so; struct file *fp; int family, type, protocol; int error, fd; if (td->td_dupfd >= 0) return ENODEV; switch (dev2unit(dev)) { case dev_udp: family = AF_INET; type = SOCK_DGRAM; protocol = IPPROTO_UDP; break; case dev_tcp: family = AF_INET; type = SOCK_STREAM; protocol = IPPROTO_TCP; break; case dev_ip: case dev_rawip: family = AF_INET; type = SOCK_RAW; protocol = IPPROTO_IP; break; case dev_icmp: family = AF_INET; type = SOCK_RAW; protocol = IPPROTO_ICMP; break; case dev_unix_dgram: family = AF_LOCAL; type = SOCK_DGRAM; protocol = 0; break; case dev_unix_stream: case dev_unix_ord_stream: family = AF_LOCAL; type = SOCK_STREAM; protocol = 0; break; case dev_ptm: return svr4_ptm_alloc(td); default: return EOPNOTSUPP; } fdp = td->td_proc->p_fd; if ((error = falloc(td, &fp, &fd, 0)) != 0) return error; /* An extra reference on `fp' has been held for us by falloc(). */ error = socreate(family, &so, type, protocol, td->td_ucred, td); if (error) { fdclose(fdp, fp, fd, td); fdrop(fp, td); return error; } finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &svr4_netops); /* * Allocate a stream structure and attach it to this socket. * We don't bother locking so_emuldata for SVR4 stream sockets as * its value is constant for the lifetime of the stream once it * is initialized here. */ st = malloc(sizeof(struct svr4_strm), M_TEMP, M_WAITOK); st->s_family = so->so_proto->pr_domain->dom_family; st->s_cmd = ~0; st->s_afd = -1; st->s_eventmask = 0; so->so_emuldata = st; fdrop(fp, td); td->td_dupfd = fd; return ENXIO; }
void PROTOCOL_Load(int no_dlg) { (void)no_dlg; #ifdef MODULAR if(! PROTOCOL_HasModule(Model.protocol)) { *loaded_protocol = 0; return; } if(*loaded_protocol == Model.protocol) return; char file[25]; strcpy(file, "protocol/"); #define PROTODEF(proto, module, map, cmd, name) case proto: strcat(file,name); break; switch(Model.protocol) { #include "protocol.h" default: *loaded_protocol = 0; return; } #undef PROTODEF file[17] = '\0'; //truncate filename to 8 characters strcat(file, ".mod"); FILE *fh; //We close the current font because on the dveo8 we reuse //the font filehandle to read the protocol. //Thatis necessary because we need to be able to load the //protocol while an ini file is open, and we don't want to //waste the RAM for an extra filehandle u8 old_font = LCD_SetFont(0); finit(&FontFAT, ""); //In case no fonts are loaded yet fh = fopen2(&FontFAT, file, "r"); //printf("Loading %s: %08lx\n", file, fh); if(! fh) { if(! no_dlg) { sprintf(tempstring, "Misisng protocol:\n%s", file); PAGE_ShowWarning(NULL, tempstring); } LCD_SetFont(old_font); return; } setbuf(fh, 0); int size = 0; unsigned char buf[256]; int len; char *ptr = (char *)loaded_protocol; while(size < 4096) { len = fread(buf, 1, 256, fh); if(len) { memcpy(ptr, buf, len); ptr += len; } size += len; if (len != 256) break; } fclose(fh); LCD_SetFont(old_font); if ((unsigned long)&_data_loadaddr != *loaded_protocol) { if(! no_dlg) { sprintf(tempstring, "Protocol Mismatch:\n%08x\n%08x", (unsigned long)&_data_loadaddr, *loaded_protocol); PAGE_ShowWarning(NULL, tempstring); } *loaded_protocol = 0; return; } //printf("Updated %d (%d) bytes: Data: %08lx %08lx %08lx\n", size, len, *loaded_protocol, *(loaded_protocol+1), *(loaded_protocol+2)); //We use the same file for multiple protocols, so we need to manually set this here *loaded_protocol = Model.protocol; #else if(! PROTOCOL_HasModule(Model.protocol)) { PROTO_Cmds = NULL; printf("Module is not defined!\n"); return; } #define PROTODEF(proto, module, map, cmd, name) case proto: PROTO_Cmds = cmd; break; switch(Model.protocol) { #include "protocol.h" default: PROTO_Cmds = NULL; } #undef PROTODEF #endif PROTOCOL_SetSwitch(get_module(Model.protocol)); }
static int math_emulate(struct trapframe * tframe) { unsigned char FPU_modrm; unsigned short code; #ifdef LOOKAHEAD_LIMIT int lookahead_limit = LOOKAHEAD_LIMIT; #endif #ifdef PARANOID if (emulating) { printf("ERROR: wm-FPU-emu is not RE-ENTRANT!\n"); } REENTRANT_CHECK(ON); #endif /* PARANOID */ if ((((struct pcb *) curproc->p_addr)->pcb_flags & FP_SOFTFP) == 0) { finit(); control_word = __INITIAL_NPXCW__; ((struct pcb *) curproc->p_addr)->pcb_flags |= FP_SOFTFP; } FPU_info = tframe; FPU_ORIG_EIP = FPU_EIP; /* --pink-- */ if (FPU_CS != 0x001f) { printf("math_emulate: %x : %x\n", FPU_CS, FPU_EIP); panic("FPU emulation in kernel"); } #ifdef notyet /* We cannot handle emulation in v86-mode */ if (FPU_EFLAGS & 0x00020000) { FPU_ORIG_EIP = FPU_EIP; math_abort(FPU_info, SIGILL); } #endif FPU_lookahead = FPU_LOOKAHEAD; if (curproc->p_flag & P_TRACED) FPU_lookahead = 0; do_another_FPU_instruction: REENTRANT_CHECK(OFF); code = fuword((u_int *) FPU_EIP); REENTRANT_CHECK(ON); if ((code & 0xff) == 0x9b) { /* fwait */ if (status_word & SW_Summary) goto do_the_FPU_interrupt; else { FPU_EIP++; goto FPU_instruction_done; } } if (status_word & SW_Summary) { /* Ignore the error for now if the current instruction is a * no-wait control instruction */ /* The 80486 manual contradicts itself on this topic, so I use * the following list of such instructions until I can check * on a real 80486: fninit, fnstenv, fnsave, fnstsw, fnstenv, * fnclex. */ if (!((((code & 0xf803) == 0xe003) || /* fnclex, fninit, * fnstsw */ (((code & 0x3003) == 0x3001) && /* fnsave, fnstcw, * fnstenv, fnstsw */ ((code & 0xc000) != 0xc000))))) { /* This is a guess about what a real FPU might do to * this bit: */ /* status_word &= ~SW_Summary; ****/ /* We need to simulate the action of the kernel to FPU * interrupts here. Currently, the "real FPU" part of * the kernel (0.99.10) clears the exception flags, * sets the registers to empty, and passes information * back to the interrupted process via the cs selector * and operand selector, so we do the same. */ do_the_FPU_interrupt: cs_selector &= 0xffff0000; cs_selector |= (status_word & ~SW_Top) | ((top & 7) << SW_Top_Shift); operand_selector = tag_word(); status_word = 0; top = 0; { int r; for (r = 0; r < 8; r++) { regs[r].tag = TW_Empty; } } REENTRANT_CHECK(OFF); math_abort(SIGFPE); } } FPU_entry_eip = FPU_ORIG_EIP = FPU_EIP; if ((code & 0xff) == 0x66) { /* size prefix */ FPU_EIP++; REENTRANT_CHECK(OFF); code = fuword((u_int *) FPU_EIP); REENTRANT_CHECK(ON); } FPU_EIP += 2; FPU_modrm = code >> 8; FPU_rm = FPU_modrm & 7; if (FPU_modrm < 0300) { /* All of these instructions use the mod/rm byte to get a data * address */ get_address(FPU_modrm); if (!(code & 1)) { unsigned short status1 = status_word; FPU_st0_ptr = &st(0); FPU_st0_tag = FPU_st0_ptr->tag; /* Stack underflow has priority */ if (NOT_EMPTY_0) { switch ((code >> 1) & 3) { case 0: reg_load_single(); break; case 1: reg_load_int32(); break; case 2: reg_load_double(); break; case 3: reg_load_int16(); break; } /* No more access to user memory, it is safe * to use static data now */ FPU_st0_ptr = &st(0); FPU_st0_tag = FPU_st0_ptr->tag; /* NaN operands have the next priority. */ /* We have to delay looking at st(0) until * after loading the data, because that data * might contain an SNaN */ if ((FPU_st0_tag == TW_NaN) || (FPU_loaded_data.tag == TW_NaN)) { /* Restore the status word; we might * have loaded a denormal. */ status_word = status1; if ((FPU_modrm & 0x30) == 0x10) { /* fcom or fcomp */ EXCEPTION(EX_Invalid); setcc(SW_C3 | SW_C2 | SW_C0); if (FPU_modrm & 0x08) pop(); /* fcomp, so we pop. */ } else real_2op_NaN(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr); goto reg_mem_instr_done; } switch ((FPU_modrm >> 3) & 7) { case 0: /* fadd */ reg_add(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr, control_word); break; case 1: /* fmul */ reg_mul(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr, control_word); break; case 2: /* fcom */ compare_st_data(); break; case 3: /* fcomp */ compare_st_data(); pop(); break; case 4: /* fsub */ reg_sub(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr, control_word); break; case 5: /* fsubr */ reg_sub(&FPU_loaded_data, FPU_st0_ptr, FPU_st0_ptr, control_word); break; case 6: /* fdiv */ reg_div(FPU_st0_ptr, &FPU_loaded_data, FPU_st0_ptr, control_word); break; case 7: /* fdivr */ if (FPU_st0_tag == TW_Zero) status_word = status1; /* Undo any denorm tag, * zero-divide has * priority. */ reg_div(&FPU_loaded_data, FPU_st0_ptr, FPU_st0_ptr, control_word); break; } } else { if ((FPU_modrm & 0x30) == 0x10) {
/* System calls. */ int sys_shm_open(struct thread *td, struct shm_open_args *uap) { struct filedesc *fdp; struct shmfd *shmfd; struct file *fp; char *path; Fnv32_t fnv; mode_t cmode; int fd, error; #ifdef CAPABILITY_MODE /* * shm_open(2) is only allowed for anonymous objects. */ if (IN_CAPABILITY_MODE(td) && (uap->path != SHM_ANON)) return (ECAPMODE); #endif if ((uap->flags & O_ACCMODE) != O_RDONLY && (uap->flags & O_ACCMODE) != O_RDWR) return (EINVAL); if ((uap->flags & ~(O_ACCMODE | O_CREAT | O_EXCL | O_TRUNC)) != 0) return (EINVAL); fdp = td->td_proc->p_fd; cmode = (uap->mode & ~fdp->fd_cmask) & ACCESSPERMS; error = falloc(td, &fp, &fd, 0); if (error) return (error); /* A SHM_ANON path pointer creates an anonymous object. */ if (uap->path == SHM_ANON) { /* A read-only anonymous object is pointless. */ if ((uap->flags & O_ACCMODE) == O_RDONLY) { fdclose(fdp, fp, fd, td); fdrop(fp, td); return (EINVAL); } shmfd = shm_alloc(td->td_ucred, cmode); } else { path = malloc(MAXPATHLEN, M_SHMFD, M_WAITOK); error = copyinstr(uap->path, path, MAXPATHLEN, NULL); /* Require paths to start with a '/' character. */ if (error == 0 && path[0] != '/') error = EINVAL; if (error) { fdclose(fdp, fp, fd, td); fdrop(fp, td); free(path, M_SHMFD); return (error); } fnv = fnv_32_str(path, FNV1_32_INIT); sx_xlock(&shm_dict_lock); shmfd = shm_lookup(path, fnv); if (shmfd == NULL) { /* Object does not yet exist, create it if requested. */ if (uap->flags & O_CREAT) { #ifdef MAC error = mac_posixshm_check_create(td->td_ucred, path); if (error == 0) { #endif shmfd = shm_alloc(td->td_ucred, cmode); shm_insert(path, fnv, shmfd); #ifdef MAC } #endif } else { free(path, M_SHMFD); error = ENOENT; } } else { /* * Object already exists, obtain a new * reference if requested and permitted. */ free(path, M_SHMFD); if ((uap->flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) error = EEXIST; else { #ifdef MAC error = mac_posixshm_check_open(td->td_ucred, shmfd, FFLAGS(uap->flags & O_ACCMODE)); if (error == 0) #endif error = shm_access(shmfd, td->td_ucred, FFLAGS(uap->flags & O_ACCMODE)); } /* * Truncate the file back to zero length if * O_TRUNC was specified and the object was * opened with read/write. */ if (error == 0 && (uap->flags & (O_ACCMODE | O_TRUNC)) == (O_RDWR | O_TRUNC)) { #ifdef MAC error = mac_posixshm_check_truncate( td->td_ucred, fp->f_cred, shmfd); if (error == 0) #endif shm_dotruncate(shmfd, 0); } if (error == 0) shm_hold(shmfd); } sx_xunlock(&shm_dict_lock); if (error) { fdclose(fdp, fp, fd, td); fdrop(fp, td); return (error); } } finit(fp, FFLAGS(uap->flags & O_ACCMODE), DTYPE_SHM, shmfd, &shm_ops); FILEDESC_XLOCK(fdp); if (fdp->fd_ofiles[fd] == fp) fdp->fd_ofileflags[fd] |= UF_EXCLOSE; FILEDESC_XUNLOCK(fdp); td->td_retval[0] = fd; fdrop(fp, td); return (0); }
Script::~Script() { finit(); }
/* Other helper routines. */ static int ksem_create(struct thread *td, const char *name, semid_t *semidp, mode_t mode, unsigned int value, int flags, int compat32) { struct filedesc *fdp; struct ksem *ks; struct file *fp; char *path; Fnv32_t fnv; int error, fd; if (value > SEM_VALUE_MAX) return (EINVAL); fdp = td->td_proc->p_fd; mode = (mode & ~fdp->fd_cmask) & ACCESSPERMS; error = falloc(td, &fp, &fd, O_CLOEXEC); if (error) { if (name == NULL) error = ENOSPC; return (error); } /* * Go ahead and copyout the file descriptor now. This is a bit * premature, but it is a lot easier to handle errors as opposed * to later when we've possibly created a new semaphore, etc. */ error = ksem_create_copyout_semid(td, semidp, fd, compat32); if (error) { fdclose(fdp, fp, fd, td); fdrop(fp, td); return (error); } if (name == NULL) { /* Create an anonymous semaphore. */ ks = ksem_alloc(td->td_ucred, mode, value); if (ks == NULL) error = ENOSPC; else ks->ks_flags |= KS_ANONYMOUS; } else { path = malloc(MAXPATHLEN, M_KSEM, M_WAITOK); error = copyinstr(name, path, MAXPATHLEN, NULL); /* Require paths to start with a '/' character. */ if (error == 0 && path[0] != '/') error = EINVAL; if (error) { fdclose(fdp, fp, fd, td); fdrop(fp, td); free(path, M_KSEM); return (error); } fnv = fnv_32_str(path, FNV1_32_INIT); sx_xlock(&ksem_dict_lock); ks = ksem_lookup(path, fnv); if (ks == NULL) { /* Object does not exist, create it if requested. */ if (flags & O_CREAT) { ks = ksem_alloc(td->td_ucred, mode, value); if (ks == NULL) error = ENFILE; else { ksem_insert(path, fnv, ks); path = NULL; } } else error = ENOENT; } else { /* * Object already exists, obtain a new * reference if requested and permitted. */ if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) error = EEXIST; else { #ifdef MAC error = mac_posixsem_check_open(td->td_ucred, ks); if (error == 0) #endif error = ksem_access(ks, td->td_ucred); } if (error == 0) ksem_hold(ks); #ifdef INVARIANTS else ks = NULL; #endif } sx_xunlock(&ksem_dict_lock); if (path) free(path, M_KSEM); } if (error) { KASSERT(ks == NULL, ("ksem_create error with a ksem")); fdclose(fdp, fp, fd, td); fdrop(fp, td); return (error); } KASSERT(ks != NULL, ("ksem_create w/o a ksem")); finit(fp, FREAD | FWRITE, DTYPE_SEM, ks, &ksem_ops); fdrop(fp, td); return (0); }