int net_timer_init(void) { int err; net_q.next = net_q.prev = (net_timer_event *)&net_q; err = hal_mutex_init(&net_q.lock, "net timer mutex"); if(err < 0) return err; //net_q.wait_sem = sem_create(0, "net timer wait sem"); if( hal_sem_init(&(net_q.wait_sem), "NetQ") < 0 ) { mutex_destroy(&net_q.lock); return -1; //net_q.wait_sem; } //net_q.runner_thread = thread_create_kernel_thread("net timer runner", &net_timer_runner, NULL); net_q.runner_thread = hal_start_kernel_thread_arg( &net_timer_runner, NULL ); if(net_q.runner_thread < 0) { sem_delete(net_q.wait_sem); mutex_destroy(&net_q.lock); return -1; //net_q.runner_thread; } //thread_resume_thread(net_q.runner_thread); return 0; }
void dpc_init() { SHOW_FLOW0( 1, "Starting DPC"); hal_cond_init( &dpc_thread_sleep_stone, "DPC" ); spinlock_init( &dpc_request_lock ); hal_mutex_init(&unused_dpc_mutex, "DPC"); SHOW_FLOW0( 1, "Starting DPC thread..."); //dpc_thread_object = hal_start_kernel_thread(dpc_thread); #if !DPC_WAKE_TIMER SHOW_FLOW0( 1, " starting DPC waker thread..."); //dpc_timed_waker_thread_object = hal_start_kernel_thread(dpc_timed_waker_thread); #else dpc_timer_wake(0); #endif while(!dpc_init_ok) { SHOW_FLOW0( 2, " wait for DPC threads..."); hal_sleep_msec(1); // wait for thread to start } SHOW_FLOW0( 0, "DPC start done"); }
static errno_t threads_test() { hal_cond_init(&c, "threadTest"); hal_mutex_init(&m, "threadTest"); hal_sem_init(&s, "threadTest"); int i = 40; n_t_empty = i; while(i-- > 0) phantom_create_thread( t_empty, "Empty", 0 ); pressEnter("will create thread"); phantom_create_thread( thread1, "__T1__", 0 ); phantom_create_thread( thread1, "__T2__", 0 ); //phantom_create_thread( thread1, "__T3__" ); //phantom_create_thread( t_wait, "__TW__" ); int tid = hal_start_kernel_thread_arg( t_wait, "__TW__" ); i = 40; while(i-- > 0) { if(TEST_CHATTY) pressEnter("will yield"); YIELD(); if(TEST_CHATTY) printf("!! back in main\n"); } t_kill_thread( tid ); hal_sleep_msec( 30 ); thread_stop_request = 1; hal_sleep_msec( 10 ); thread_activity_counter = 0; hal_sleep_msec( 1000 ); if( thread_activity_counter ) { SHOW_ERROR0( 0, "Can't stop thread" ); return -1; } while(n_t_empty > 0) { SHOW_FLOW( 0, "wait for %d threads", n_t_empty ); hal_sleep_msec(500); } if(p._ah.refCount != 1) { SHOW_ERROR( 0, "p._ah.refCount = %d", p._ah.refCount ); test_fail_msg( -1, "refcount" ); } else SHOW_ERROR( 0, "p._ah.refCount = %d, SUCCESS", p._ah.refCount ); return 0; }
void phantom_unix_fs_init() { hal_mutex_init( &mm, "rootfs" ); add_mount( "/dev", "devfs", &dev_fs ); add_mount( "/proc", "procfs", &proc_fs ); add_mount( "udp://", "udpfs", &udp_fs ); add_mount( "tcp://", "tcpfs", &tcp_fs ); }
void pvm_internal_init_weakref(struct pvm_object_storage * os) { struct data_area_4_weakref * da = (struct data_area_4_weakref *)os->da; da->object.data = 0; #if WEAKREF_SPIN hal_spin_init( &da->lock ); #else hal_mutex_init( &da->mutex, "WeakRef" ); #endif }
void phantom_unix_proc_init(void) { int i; hal_mutex_init(&proc_lock, "process"); for( i = 0; i < MAX_UU_PROC; i++ ) proc[i].pid = -1; // add the debug command dbg_add_command(&dbg_ps, "ps", "Process list"); }
void resolver_init() { hal_mutex_init(&rcache_mutex, "resolver"); /* rcache = hash_init(256, offsetof(rcache_entry, next), &rcache_compare, &rcache_hash); if(!rcache) return -1; */ inited = 1; }
static void deferred_refdec_init(void) { hal_mutex_init( &deferred_refdec_mutex, "refdec"); hal_cond_init( &start_refdec_cond, "refdec st" ); hal_cond_init( &end_refdec_cond, "refdec end" ); deferred_refdec_thread_id = hal_start_thread( deferred_refdec_thread, 0, 0 ); assert(deferred_refdec_thread_id > 0 ); inited = 1; }
errno_t udp_init(void) { hal_mutex_init(&endpoints_lock, "udp_endpoints"); endpoints = hash_init(256, offsetof(udp_endpoint, next), &udp_endpoint_compare_func, &udp_endpoint_hash_func); assert(endpoints); if(!endpoints) return ENOMEM; return 0; }
void phantom_snap_threads_interlock_init( void ) { #if !NEW_SNAP_SYNC if( hal_cond_init( &phantom_snap_wait_4_vm_enter, "Snap W4E" ) || hal_cond_init( &phantom_snap_wait_4_vm_leave, "Snap W4L" ) || hal_cond_init( &phantom_vm_wait_4_snap, "Snap W4S" ) || hal_mutex_init( &interlock_mutex, "Snap ILck" ) || hal_cond_init( &vm_thread_wakeup_cond, "VmSleep") ) panic("Can't init thread/snap interlocks"); #endif }
void init_main_event_q() { queue_init(&ev_unused_events); queue_init(&ev_main_event_q); hal_mutex_init( &ev_main_q_mutex, "Main EvQ" ); hal_mutex_init( &ev_unused_q_mutex, "Free EvQ" ); hal_cond_init( &ev_have_event, "UIEvent" ); hal_mutex_lock( &ev_unused_q_mutex ); int i = MIN_EVENT_POOL; while(i--) ev_allocate_event(); hal_mutex_unlock( &ev_unused_q_mutex ); #if EVENTS_ENABLED hal_start_kernel_thread( ev_push_thread ); ev_engine_active = 1; #if KEY_EVENTS phantom_set_console_getchar( phantom_window_getc ); hal_start_kernel_thread( ev_keyboard_read_thread ); #endif #endif #if DELIVER2THREAD hal_start_kernel_thread( w_event_deliver_thread ); #endif #if VIDEO_NEW_BG_WIN w_bg_win_init(); #endif }
int if_init(void) { int err; next_id = 0; // create a hash table to store the interface list ifhash = hash_init(16, offsetof(ifnet, next), &if_compare_func, &if_hash_func); err = hal_mutex_init(&ifhash_lock, "if list"); if(err < 0) return err; return NO_ERROR; }
int udp_open(void **prot_data) { udp_endpoint *e = calloc( sizeof(udp_endpoint), 1 ); if(!e) return ERR_NO_MEMORY; hal_mutex_init(&e->lock, "UDP endp"); //e->blocking_sem = sem_create(0, "udp endpoint sem"); hal_sem_init(&(e->blocking_sem), "UDP blk"); e->port = 0; e->ref_count = 1; udp_init_queue(&e->q); mutex_lock(&endpoints_lock); hash_insert(endpoints, e); mutex_unlock(&endpoints_lock); *prot_data = e; return 0; }
static errno_t cache_do_init( cache_t *c, size_t page_size ) { memset( c, sizeof(cache_t), 0 ); hal_mutex_init( &c->lock, "DiskCache" ); hal_mutex_lock( &c->lock ); c->page_size = page_size; queue_init( &c->lru ); unsigned int table_size = 2048; c->hash = hash_init( table_size, offsetof(cache_el_t, hash_next), c_el_compare_func, c_el_hash_func ); cache_do_create_els( c, 1024 ); hal_mutex_unlock( &c->lock ); return 0; }
wtty_t * wtty_init( size_t bufferSize ) { wtty_t *w = calloc( 1, sizeof(wtty_t) ); assert(w); #if CONF_WTTY_SIZE //w->size = WTTY_DEFAULT_BUFSIZE; // make to be param, check w->size = bufferSize; if( w->size < WTTY_MIN_BUFSIZE ) w->size = WTTY_MIN_BUFSIZE; w->buf = calloc( 1, w->size ); assert(w->buf); #endif //w->getpos = 0; //w->putpos = 0; hal_mutex_init( &w->mutex, "wtty" ); hal_cond_init( &w->rcond, "wtty.r" ); hal_cond_init( &w->wcond, "wtty.w" ); w->started = 1; return w; }
// type is IF_TYPE_LOOPBACK, IF_TYPE_ETHERNET //int if_register_interface(const char *path, ifnet **_i) int if_register_interface(int type, ifnet **_i, phantom_device_t *dev) { ifnet *i; //int type; int err; ifaddr *address; i = kmalloc(sizeof(ifnet)); if(!i) { err = ERR_NO_MEMORY; goto err; } memset(i, 0, sizeof(ifnet)); i->dev = dev; if(dev != 0 && (i->dev->dops.write == 0 || i->dev->dops.read == 0) ) panic("dev has no read or write!"); /* open the device * / if(!strcmp(path, "loopback")) { // the 'loopback' device is special type = IF_TYPE_LOOPBACK; i->fd = -1; } else { i->fd = sys_open(path, 0); if(i->fd < 0) { err = i->fd; goto err1; } // find the device's type err = sys_ioctl(i->fd, IOCTL_NET_IF_GET_TYPE, &type, sizeof(type)); if(err < 0) { goto err2; } } */ // find the appropriate function calls to the link layer drivers switch(type) { case IF_TYPE_LOOPBACK: i->link_input = &loopback_input; i->link_output = &loopback_output; i->mtu = 65535; break; case IF_TYPE_ETHERNET: assert(dev); i->link_input = ðernet_input; i->link_output = ðernet_output; i->mtu = ETHERNET_MAX_SIZE - ETHERNET_HEADER_SIZE; /* bind the ethernet link address */ address = kmalloc(sizeof(ifaddr)); address->addr.len = 6; address->addr.type = ADDR_TYPE_ETHERNET; if( dev->dops.get_address == 0 ) { err = ERR_NET_GENERAL; kfree(address); goto err2; } err = dev->dops.get_address(dev, &address->addr.addr[0], 6); if(err < 0) { err = ERR_NET_GENERAL; kfree(address); goto err2; } /*err = sys_ioctl(i->fd, IOCTL_NET_IF_GET_ADDR, &address->addr.addr[0], 6); if(err < 0) { kfree(address); goto err2; }*/ address->broadcast.len = 6; address->broadcast.type = ADDR_TYPE_ETHERNET; memset(&address->broadcast.addr[0], 0xff, 6); address->netmask.type = ADDR_TYPE_NULL; if_bind_link_address(i, address); break; default: err = ERR_NET_GENERAL; goto err1; } i->id = ATOMIC_ADD_AND_FETCH(&next_id, 1); //strlcpy(i->path, path, sizeof(i->path)); i->type = type; i->rx_thread = -1; i->tx_thread = -1; //i->tx_queue_sem = sem_create(0, "tx_queue_sem"); hal_sem_init( &(i->tx_queue_sem), "IF TX sem" ); hal_mutex_init(&i->tx_queue_lock, "IF TX Q"); fixed_queue_init(&i->tx_queue, TX_QUEUE_SIZE); mutex_lock(&ifhash_lock); hash_insert(ifhash, i); mutex_unlock(&ifhash_lock); // don't need threads for loopback if if(type != IF_TYPE_LOOPBACK) { /* start the rx and tx threads on this interface */ err = if_boot_interface(i); if(err < 0) goto err2; //bootp(i); } *_i = i; return NO_ERROR; err2: //sys_close(i->fd); err1: kfree(i); err: return err; }
static int rtl8169_init(rtl8169 *r) { //bigtime_t time; int err = -1; //addr_t temp; //int i; hal_mutex_init(&r->lock,DEBUG_MSG_PREFIX); SHOW_FLOW(2, "rtl8169_init: r %p\n", r); /* r->region = vm_map_physical_memory(vm_get_kernel_aspace_id(), "rtl8169_region", (void **)&r->virt_base, REGION_ADDR_ANY_ADDRESS, r->phys_size, LOCK_KERNEL|LOCK_RW, r->phys_base); if(r->region < 0) { SHOW_ERROR0(1, "rtl8169_init: error creating memory mapped region\n"); err = -1; goto err; }*/ size_t n_pages = BYTES_TO_PAGES(r->phys_size); hal_alloc_vaddress( (void **)&r->virt_base, n_pages); // alloc address of a page, but not memory hal_pages_control_etc( r->phys_base, (void *)r->virt_base, n_pages, page_map_io, page_rw, 0 ); SHOW_INFO(2, "rtl8169 mapped at address 0x%lx\n", r->virt_base); #if 0 /* create regions for tx and rx descriptors */ r->rxdesc_region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "rtl8169_rxdesc", (void **)&r->rxdesc, REGION_ADDR_ANY_ADDRESS, NUM_RX_DESCRIPTORS * DESCRIPTOR_LEN, REGION_WIRING_WIRED_CONTIG, LOCK_KERNEL|LOCK_RW); r->rxdesc_phys = vtophys(r->rxdesc); SHOW_INFO(2, "rtl8169: rx descriptors at %p, phys 0x%x\n", r->rxdesc, r->rxdesc_phys); r->txdesc_region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "rtl8169_txdesc", (void **)&r->txdesc, REGION_ADDR_ANY_ADDRESS, NUM_TX_DESCRIPTORS * DESCRIPTOR_LEN, REGION_WIRING_WIRED_CONTIG, LOCK_KERNEL|LOCK_RW); r->txdesc_phys = vtophys(r->txdesc); SHOW_INFO(2, "rtl8169: tx descriptors at %p, phys 0x%x\n", r->txdesc, r->txdesc_phys); r->reg_spinlock = 0; /* create a large tx and rx buffer for the descriptors to point to */ r->rxbuf_region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "rtl8169_rxbuf", (void **)&r->rxbuf, REGION_ADDR_ANY_ADDRESS, NUM_RX_DESCRIPTORS * BUFSIZE_PER_FRAME, REGION_WIRING_WIRED, LOCK_KERNEL|LOCK_RW); r->txbuf_region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "rtl8169_txbuf", (void **)&r->txbuf, REGION_ADDR_ANY_ADDRESS, NUM_TX_DESCRIPTORS * BUFSIZE_PER_FRAME, REGION_WIRING_WIRED, LOCK_KERNEL|LOCK_RW); #endif hal_pv_alloc( &r->rxdesc_phys, (void**)&r->rxdesc, NUM_RX_DESCRIPTORS * DESCRIPTOR_LEN ); hal_pv_alloc( &r->txdesc_phys, (void**)&r->txdesc, NUM_TX_DESCRIPTORS * DESCRIPTOR_LEN ); SHOW_INFO(2, "rx descriptors at %p, phys 0x%x\n", r->rxdesc, r->rxdesc_phys); SHOW_INFO(2, "tx descriptors at %p, phys 0x%x\n", r->txdesc, r->txdesc_phys); hal_pv_alloc( &r->rxbuf_phys, (void**)&r->rxbuf, NUM_RX_DESCRIPTORS * BUFSIZE_PER_FRAME ); hal_pv_alloc( &r->txbuf_phys, (void**)&r->txbuf, NUM_TX_DESCRIPTORS * BUFSIZE_PER_FRAME ); /* create a receive sem */ hal_sem_init( &r->rx_sem, "rtl8169 rx_sem"); /* transmit sem */ hal_sem_init( &r->tx_sem, "rtl8169 tx_sem"); /* reset the chip */ int repeats = 100; RTL_WRITE_8(r, REG_CR, (1<<4)); // reset the chip, disable tx/rx do { hal_sleep_msec(10); // 10ms if(repeats -- <= 0 ) break; } while(RTL_READ_8(r, REG_CR) & (1<<4)); /* read in the mac address */ r->mac_addr[0] = RTL_READ_8(r, REG_IDR0); r->mac_addr[1] = RTL_READ_8(r, REG_IDR1); r->mac_addr[2] = RTL_READ_8(r, REG_IDR2); r->mac_addr[3] = RTL_READ_8(r, REG_IDR3); r->mac_addr[4] = RTL_READ_8(r, REG_IDR4); r->mac_addr[5] = RTL_READ_8(r, REG_IDR5); SHOW_INFO(2, "rtl8169: mac addr %x:%x:%x:%x:%x:%x\n", r->mac_addr[0], r->mac_addr[1], r->mac_addr[2], r->mac_addr[3], r->mac_addr[4], r->mac_addr[5]); /* some voodoo from BSD driver */ RTL_WRITE_16(r, REG_CCR, RTL_READ_16(r, REG_CCR)); RTL_SETBITS_16(r, REG_CCR, 0x3); /* mask all interrupts */ RTL_WRITE_16(r, REG_IMR, 0); /* set up the tx/rx descriptors */ rtl8169_setup_descriptors(r); /* enable tx/rx */ RTL_SETBITS_8(r, REG_CR, (1<<3)|(1<<2)); /* set up the rx state */ /* 1024 byte dma threshold, 1024 dma max burst, CRC calc 8 byte+, accept all packets */ RTL_WRITE_32(r, REG_RCR, (1<<16) | (6<<13) | (6<<8) | (0xf << 0)); RTL_SETBITS_16(r, REG_CCR, (1<<5)); // rx checksum enable RTL_WRITE_16(r, REG_RMS, 1518); // rx mtu /* set up the tx state */ RTL_WRITE_32(r, REG_TCR, (RTL_READ_32(r, REG_TCR) & ~0x1ff) | (6<<8)); // 1024 max burst dma RTL_WRITE_8(r, REG_MTPS, 0x3f); // max tx packet size (must be careful to not actually transmit more than mtu) /* set up the interrupt handler */ //int_set_io_interrupt_handler(r->irq, &rtl8169_int, r, "rtl8169"); if(hal_irq_alloc( r->irq, &rtl8169_int, r, HAL_IRQ_SHAREABLE )) { SHOW_ERROR( 0, "unable to allocate irq %d", r->irq ); goto err1; } /* clear all pending interrupts */ RTL_WRITE_16(r, REG_ISR, 0xffff); /* unmask interesting interrupts */ RTL_WRITE_16(r, REG_IMR, IMR_SYSERR | IMR_LINKCHG | IMR_TER | IMR_TOK | IMR_RER | IMR_ROK | IMR_RXOVL); return 0; err1: // TODO free what? //vm_delete_region(vm_get_kernel_aspace_id(), r->region); //err: return err; }
static void paint_q_init(void) { hal_mutex_init( &rect_list_lock, "rectList" ); queue_init(&rect_list); }
void phantom_init_console_window() { hal_mutex_init( &buf_mutex, "console" ); console_fg = COLOR_LIGHTGRAY; console_bg = COLOR_BLACK; int xsize = 620, ysize = 300; int cw_x = 50, cw_y = 450; if( scr_get_ysize() < 600 ) { cw_x = cw_y = 0; } drv_video_window_t *w = drv_video_window_create( xsize, ysize, cw_x, cw_y, console_bg, "Console", WFLAG_WIN_DECORATED|WFLAG_WIN_DOUBLEBUF ); phantom_console_window = w; w->owner = get_current_tid(); phantom_set_console_ops( &win_ops ); phantom_console_window_puts("Phantom console window\n"); phantom_debug_window = drv_video_window_create( DEBWIN_XS, DEBWIN_YS, DEBWIN_X, DEBWIN_Y, console_bg, "Threads", WFLAG_WIN_DECORATED|WFLAG_WIN_DOUBLEBUF|WFLAG_WIN_FULLPAINT ); //phantom_debug_window->flags |= WFLAG_WIN_DOUBLEBUF|WFLAG_WIN_FULLPAINT; //w_update( phantom_debug_window ); // For dbl buf flags to start working ok phantom_debug_window_puts("Phantom debug window\n\nt - threads\nw - windows\ns - stats\np - profiler\n"); w_update( phantom_debug_window ); //hal_sleep_msec(4000); hal_start_kernel_thread(phantom_debug_window_loop); // ------------------------------------------------------------------- // Launcher window // ------------------------------------------------------------------- color_t la_bg = { 0x19, 0x19, 0x19, 0xFF }; color_t la_b1 = { 68, 66, 62, 0xFF }; color_t la_b2 = { 88, 84, 79, 0xFF }; color_t la_txt = { 0x11, 0xd5, 0xff, 0xFF }; //#define BTEXT_COLOR COLOR_YELLOW #define BTEXT_COLOR la_txt phantom_launcher_window = drv_video_window_create( scr_get_xsize(), 32, 0, 0, console_bg, "Launcher", WFLAG_WIN_ONTOP ); phantom_launcher_window->inKernelEventProcess = phantom_launcher_event_process; w_fill( phantom_launcher_window, la_bg ); int lb_x = scr_get_xsize(); lb_x -= power_button_sm_bmp.xsize + 5; w_add_button( phantom_launcher_window, -1, lb_x, 2, &power_button_sm_bmp, &power_button_pressed_sm_bmp, BUTTON_FLAG_NOBORDER ); pool_handle_t bh; lb_x = 5; int nwin = 0; for( nwin = 0; nwin < MAX_LAUNCH_BUTTONS; nwin++ ) { char * wname = "win1"; // crashes in some configurations?? //wname[3] = '0' + nwin; bh = w_add_button( phantom_launcher_window, nwin, lb_x, 5, &task_button_bmp, &task_button_bmp, BUTTON_FLAG_NOBORDER ); w_button_set_text( phantom_launcher_window, bh, wname, BTEXT_COLOR ); lb_x += 5+task_button_bmp.xsize; taskbuttons[nwin] = bh; } w_draw_line( phantom_launcher_window, 0, 31, scr_get_xsize(), 31, la_b1 ); w_draw_line( phantom_launcher_window, 0, 30, scr_get_xsize(), 30, la_b2 ); w_update( phantom_launcher_window ); //hal_start_kernel_thread(phantom_launcher_window_loop); }