Exemplo n.º 1
0
/**
 * Intializes an ELF32-TASK
 * TODO: Check Length of image
 * TODO: US-Mode
 * @param image The image
 * @param pri Priority
 */
void* initELF(void* image, int pri, size_t size, char *args)
{
	/*
	* TODO Wir muessen eigentlich die Laenge vom Image pruefen, damit wir bei
	* korrupten ELF-Dateien nicht ueber das Dateiende hinauslesen.
	*/
	DEBUG_MSG("ELF:      Init ELF32-File...");
	struct elf_header* header = image;
	struct elf_program_header* ph;
	int i;

	/* Ist es ueberhaupt eine ELF-Datei? */
	if (header->magic != ELF_MAGIC) {
		dbg(false);
		kprintf("Keine gueltige ELF-Magic!\n");
		return 0;
	}

	/*
	* Alle Program Header durchgehen und den Speicher an die passende Stelle
	* kopieren.
	*
	* TODO Wir erlauben der ELF-Datei hier, jeden beliebigen Speicher zu
	* ueberschreiben, einschliesslich dem Kernel selbst.
	*/
	ph = (struct elf_program_header*) (((char*) image) + header->ph_offset);
	for (i = 0; i < 2/*header->ph_entry_count*/; i++, ph++) {
		void* dest = (void*) ph->virt_addr;
		void* src = ((char*) image) + ph->offset;

		/* Nur Program Header vom Typ LOAD laden */
		if (ph->type != 1)
			continue;

		kmemset(dest, 0, ph->mem_size);
		kmemcpy(dest, src, ph->file_size);
	}
	

	//size /= PAGE_SIZE;

	
	pageDir_t page = vmmCreateContext();
	void *paddr = pmm_malloc(PAGE_SIZE);
		
	kmemset((void*) page, 0, PAGE_SIZE);
	
	for(i=0; i<size;i +=PAGE_SIZE)
	{
		vmmMapPage( page, (void*) 0xfffff000+i, paddr+i, USER_PRV ); //0xfffff000
	}
	
	vmmActivateContext( page );

	
	
	initTask( (void *)header->entry, pri, page, args);
	dbg(true);
	return (void*) header->entry;
}
Exemplo n.º 2
0
static inline void
screen_monitor_one_line_scroll(void)
{
    uint8 attribute;
    uint16 blank;
    uint32 i;

    attribute = (BLACK << 4) | WHITE;
    blank = BLANK | (attribute << 8);

    if (cursor_y >= 25) {
        i = 0;

        /*
         * mov up one line
         */
        while (i < (SCREEN_Y - 1) * SCREEN_X) {
            video[i] = video[i + SCREEN_X];
            i++;
        }

        /*
         * set the last one line to blank
         */
        kmemset(&video[i], blank, SCREEN_X * sizeof(*video));
        cursor_y = SCREEN_Y - 1;
        kmemset(&video[SCREEN_X * SCREEN_Y], blank, SCREEN_X * sizeof(*video));
    }
}
Exemplo n.º 3
0
////////////////////////////////////////////////////
// 功能:
// 输入: 
// 输出:
// 返回: 
////////////////////////////////////////////////////
void Mp3BufInit(MP3_BUF *buf)
{
	buf->FHead = 0;
	buf->FTail = 0;
	kmemset(buf->FrameBuf, 0x00, MP3_FRAME_BUF_SIZE);

	buf->MTail = 0;
	buf->MHead = 0;
	buf->MainBufRemain = 0;
	buf->MainBufReady = 0;
	kmemset(buf->MainBuf, 0x00, MP3_MAIN_BUF_SIZE);
}
Exemplo n.º 4
0
////////////////////////////////////////////////////
// 功能: 
// 输入: 
// 输出:
// 返回: 
// 说明: 
////////////////////////////////////////////////////
int DacInit(void)
{
	int i;
	
	// 全局变量初始化
	hDacMutex = kMutexCreate();
	hDacSema = kSemaCreate(0);
	ListInit(&DacList);
	gVolumeSpeaker = 99;
	bHeadphoneIn = 0;
#ifdef DAC_SAVE_PCM
	dac_offset = 0;
	dac_source_offset = 0;
#endif

	//初始化结构体数据
	kmemset(&DacDevice,0,sizeof(RESAMPLE));
	kmemset((BYTE*)NullBuf,0xff,DAC_PCMBUF_SIZE);

	//初始化缓冲
	for( i = 0 ; i < MAX_PCMBUFS ; i++ )
		DacDevice.BufFlag[i] = DAC_BUF_WRITE;

	// 创建DAC设备写线程
	KernelThread(PRIO_USER-11, DacWriteThread, 0, 0);


#if defined(CONFIG_ERAPHONE_IO)
	__gpio_as_input(CONFIG_ERAPHONE_IO);
	__gpio_enable_pull(CONFIG_ERAPHONE_IO);
#endif	

	//初始化播放声音的GPIO端口
	SetMoseCe(0);

	//D类功放开机去喀嚓音的特殊处理
#if defined(CONFIG_MAC_BDS6100) || defined(CONFIG_MAC_ND800) || defined(CONFIG_MAC_ASKMI1388) || defined(CONFIG_MAC_BDS6100A) || defined(CONFIG_MAC_NP7000) \
	|| defined( CONFIG_MAC_NP2300 ) || defined(CONFIG_MAC_NP5800) || defined(CONFIG_MAC_NP6800)
	SetPowerAmplifier(1);
	SetPowerAmplifier(0);
#else
	SetPowerAmplifier(0);
#endif
#if defined(CODEC_ALWAYS_OPEN)
	MediaDraveInit(1);	//如果是CODEC一直开启模式,则初始化时打开CODEC
#endif
	return 0;
}
Exemplo n.º 5
0
Arquivo: rgn.c Projeto: vovaprog/gluKe
int rgn_init()
{
	free_als=(struct rgn_allocation*)ALS_FREE_BASE;
	reserved_als=(struct rgn_allocation*)ALS_RESERVED_BASE;

	kmemset(free_als,0,ALS_TABLE_BYTES);
	kmemset(reserved_als,0,ALS_TABLE_BYTES);

	free_als[0].base=0;
	free_als[0].nof_pages=RAM_SIZE/PAGE_SIZE;

	rgn_alloc_base(free_als,ALS_TABLE_BYTES);
	rgn_alloc_base(reserved_als,ALS_TABLE_BYTES);

	return 0;
}
Exemplo n.º 6
0
static void
cga_putc(int c)
{
  int pos;
  
  // Cursor position: col + 80*row.
  outb(CRTPORT, 14);
  pos = inb(CRTPORT+1) << 8;
  outb(CRTPORT, 15);
  pos |= inb(CRTPORT+1);

  if(c == '\n')
    pos += 80 - pos%80;
  else if(c == BACKSPACE){
    if(pos > 0)
      crt[--pos] = ' ' | 0x0700;
  } else
    crt[pos++] = (c&0xff) | 0x0700;  // black on white
  
  if((pos/80) >= 24){  // Scroll up.
    kmemmove(crt, crt+80, sizeof(crt[0])*23*80);
    pos -= 80;
    kmemset(crt+pos, 0, sizeof(crt[0])*(24*80 - pos));
  }
  
  outb(CRTPORT, 14);
  outb(CRTPORT+1, pos>>8);
  outb(CRTPORT, 15);
  outb(CRTPORT+1, pos);
  crt[pos] = ' ' | 0x0700;
}
Exemplo n.º 7
0
uint8_t pluginloader_addPluginToList(struct pluginloader_t* loader, struct loaderplugin_t* pluginEntry)
{
	if (!loader || !pluginEntry)
		return false;

	// CHeck if we have a plugin list at all
	if (!loader->pluginList || !loader->pluginCount)
	{
		// Calculate the new size (sizeof(pointer) * however many in list)
		size_t newListSize = sizeof(pluginEntry) * 1;
		struct loaderplugin_t** newList = (struct loaderplugin_t**)kmalloc(newListSize);
		if (!newList)
		{
			WriteLog(LL_Error, "could not allocate new list");
			return false;
		}

		// Assign our new entry
		newList[0] = pluginEntry;

		// Assign our new plugin list
		loader->pluginList = newList;
		loader->pluginCount = 1;

		return true;
	}

	// Am I bat-shit insane for this?... probably
	// This screams, I need locks

	// If there exist a list already add a new entry
	struct loaderplugin_t** oldList = loader->pluginList;
	size_t oldPluginCount = loader->pluginCount;
	size_t oldListSize = sizeof(struct loaderplugin_t*) * oldPluginCount;

	size_t newPluginCount = oldPluginCount + 1;
	size_t newListSize = sizeof(struct loaderplugin_t*) * (newPluginCount);
	struct loaderplugin_t** newList = (struct loaderplugin_t**)kmalloc(newListSize);
	if (!newList)
	{
		WriteLog(LL_Error, "could not allocate new list");
		return false;
	}
	kmemset(newList, 0, newListSize);

	// Copy over the old list
	kmemcpy(newList, oldList, oldListSize);

	// Add our final entry
	newList[oldPluginCount] = pluginEntry;

	// Assign everything
	loader->pluginList = newList;
	loader->pluginCount = newPluginCount;

	return true;
}
Exemplo n.º 8
0
////////////////////////////////////////////////////
// 功能:
// 输入: 
// 输出:
// 返回: 
////////////////////////////////////////////////////
static int Mp3BufGetData(MP3_BUF *mp3buf, BYTE* buf, int size)
{
	int ret;

	ret = mp3buf->pCallback(mp3buf->CallbackId, buf, size);
	if(ret < size)
		kmemset(buf + ret, 0x00, size - ret);
	
	return ret;
}
Exemplo n.º 9
0
void scroll()
{
	int i;
	for (i = 1; i < CON_ROWS; i++)
	{
		kmemcpy(cur_c->buffer + ((i - 1) * CON_COLUMNS),
				cur_c->buffer + (i * CON_COLUMNS),
				CON_COLUMNS);
	}
	kmemset(cur_c->buffer + ((CON_ROWS - 1) * CON_COLUMNS), 0, CON_COLUMNS);
}
Exemplo n.º 10
0
////////////////////////////////////////////////////
// 功能: 为音频任务申请并初始化节点
// 输入:
// 输出:
// 返回:
// 说明:
////////////////////////////////////////////////////
static PMEDIA_OBJECT MediaSrvObjCreate(void *media, char *name, int type)
{
	PMEDIA_OBJECT obj;
	
	// 输入参数检查
	if(!media)
		return NULL;

	// 申请节点,并初始化
	obj = kmalloc(sizeof(MEDIA_OBJECT));
	if(obj == NULL)
		return NULL;
	kmemset(obj, 0x00, sizeof(MEDIA_OBJECT));
	ListInit(&obj->Link);
	if(MediaSrvGetCallback(type, &obj->Cb) < 0)
	{
		kdebug(mod_media, PRINT_ERROR, "MediaSrvGetCallback error\n");
		kfree(obj);
		return NULL;
	}

	if(((PMEDIA_TASK)media)->ExterdType == 1 && JzSrvUseMplayer() < 0)
	{
		kdebug(mod_media, PRINT_ERROR, "MediaSrvGetCallback error: mplayer already exist\n");
		kfree(obj);
		return NULL;
	}
	
	// 创建媒体任务
	obj->Media = obj->Cb.MediaCreate(media);
	if(obj->Media == NULL)
	{
		kfree(obj);
		return NULL;
	}
	
	// 保存媒体名称
	if(name)
	{
		obj->MediaInfo = kmalloc(kstrlen(name)+1);
		if(obj->MediaInfo)
			kstrcpy(obj->MediaInfo, name);
	}
	
	// 设置媒体任务
#if defined(STC_EXP)
	obj->hTask = sTaskSelf();
#else
	obj->hTask = TaskSelf();
#endif	
	// 返回媒体对象
	return obj;
}
Exemplo n.º 11
0
int		kexec(t_node *file)
{
  void		*stack;
  void		*code;

  asm volatile("cli");

  /* Free the old memory */
  if (gl_current_task->brk)
    dt_memory((void *)USTACK_START, gl_current_task->brk, gl_current_task->page_dir);

  stack = allocate_place(USTACK_SIZE);
  kmemset(stack, 0, USTACK_SIZE);
  if ((code = load_elf(file)) == NULL)
    return (0);
  /* Init registers */
  kmemcpy(gl_current_task->name, file->filename, kstrlen(file->filename));
  kmemset(&gl_current_task->regs, 0, sizeof(gl_current_task->regs));
  gl_current_task->regs.cs = 0x1B;
  gl_current_task->regs.ss = 0x23;
  gl_current_task->regs.ds = 0x23;
  gl_current_task->regs.es = 0x23;
  gl_current_task->regs.fs = 0x23;
  gl_current_task->regs.gs = 0x23;
  gl_current_task->regs.esp = UESP;
  gl_current_task->regs.ebp = 0;
  gl_current_task->regs.eip = (u32)code;
  gl_current_task->regs.eflags = 0x200;
  gl_current_task->esp0 = KESP;
  gl_current_task->ss0 = 0x10;
  /* Some basic pointers - code addr, data addr, state */
  gl_current_task->p_code = code;
  gl_current_task->p_code_size = file->inode->size;
  gl_current_task->p_data = (void *)((u32)gl_current_task->p_code + file->inode->size);
  gl_current_task->brk = gl_current_task->p_data;
  gl_current_task->state = RUNNING;
  return (1);
}
Exemplo n.º 12
0
uint32_t pluginloader_getAvailablePluginCount(struct pluginloader_t* loader)
{
	if (!loader)
		return 0;

	if (!loader->pluginDirectory || loader->pluginDirectoryLength == 0)
		return 0;

	int handle = kopen(loader->pluginDirectory, 0x0000 | 0x00020000, 0);
	if (handle < 0)
	{
		WriteLog(LL_Error, "could not open plugin directory %s", loader->pluginDirectory);
		return 0;
	}

	// Run through once to get the count
	uint64_t dentCount = 0;
	struct dirent* dent = 0;
	const uint32_t bufferSize = 0x8000;
	char* buffer = kmalloc(bufferSize);
	if (!buffer)
	{
		WriteLog(LL_Error, "could not allocate memory");
		kclose(handle);
		return 0;
	}

	// Zero out the buffer size
	kmemset(buffer, 0, bufferSize);

	// Get all of the directory entries the first time to get the count
	while (kgetdents(handle, buffer, bufferSize) > 0)
	{
		dent = (struct dirent*)buffer;

		while (dent->d_fileno)
		{
			if (dent->d_type == 0)
				break;

			// Create a new plugin entry

			dent = (struct dirent*)((uint8_t*)dent + dent->d_reclen);
		}
	}

	kclose(handle);

	return dentCount;
}
Exemplo n.º 13
0
Arquivo: dt.c Projeto: dakk/Misc
/* Installs the IDT */
void idt_install()
{
    /* Sets the special IDT pointer up, just like in 'gdt.c' */
    idtp.limit = (sizeof (struct idt_entry) * 256) - 1;
    idtp.base = (unsigned int) &idt;

    /* Clear out the entire IDT, initializing it to zeros */
    kmemset(&idt, 0, sizeof(struct idt_entry) * 256);

    /* Add any new ISRs to the IDT here using idt_set_gate */

    /* Points the processor's internal register to the new IDT */
    idt_load();
}
Exemplo n.º 14
0
void pluginloader_init(struct pluginloader_t* loader)
{
	if (!loader)
		return;

	// Initialize all fields
	loader->pluginList = NULL;
	loader->pluginCount = 0;
	loader->pluginDirectory = NULL;

	// Get the currently configured framework plugin path
	char* frameworkPluginPath = gFramework->pluginsPath;
	if (!frameworkPluginPath)
	{
		WriteLog(LL_Error, "could not initialize pluginloader, plugin path not set");
		return;
	}

	// Calculate the path length
	// TODO: Unhack this
	size_t pluginPathLength = strlen(frameworkPluginPath);
	if (pluginPathLength == 0 || pluginPathLength > 260)
	{
		WriteLog(LL_Error, "path length is either zero, or > 260");
		return;
	}

	// Update the length
	loader->pluginDirectoryLength = pluginPathLength;

	// Allocate space for the plugin path
	char* pluginPath = (char*)kmalloc(pluginPathLength + 1);
	if (!pluginPath)
	{
		WriteLog(LL_Error, "could not allocate space for plugin path.");
		return;
	}
	kmemset(pluginPath, 0, pluginPathLength + 1);

	// Copy over our string
	kmemcpy(pluginPath, frameworkPluginPath, pluginPathLength);
	
	loader->pluginDirectory = pluginPath;

	// Assign our plugin loader params
	gLoaderPluginInit.framework = gFramework;
	gLoaderPluginInit.kernelBase = gKernelBase;
	gLoaderPluginInit.logger = gLogger;
}
Exemplo n.º 15
0
void	load_default_tss()
{
  u32	base;
  u32	limit;

  base = (u32)&ktss;
  limit = base + sizeof(ktss);
  gdt_set_descriptor(5, base, limit, 0xE9, 0x00);
  kmemset(&ktss, 0, sizeof(ktss));
  ktss.ldt = 0;
  ktss.esp0 = KESP;
  ktss.ss0 = 0x10;
  ktss.trap = 0x00;
  ktss.iomap_base = 0x00;
}
Exemplo n.º 16
0
/**
 * @brief Allocates a disk block.
 * 
 * @details Allocates a disk block by searching in the bitmap of blocks for a
 *          free block.
 * 
 * @param sb Superblock in which the disk block should be allocated.
 * 
 * @return Upon successful completion, the block number of the allocated block
 *         is returned. Upon failed, #BLOCK_NULL is returned instead.
 * 
 * @note The superblock must be locked.
 */
PRIVATE block_t block_alloc(struct superblock *sb)
{
	bit_t bit;          /* Bit number in the bitmap. */
	block_t num;        /* Block number.             */
	block_t blk;        /* Working block.            */
	block_t firstblk;   /* First block to check.     */
	struct buffer *buf; /* Working buffer.           */

	/* Search for a free block. */
	firstblk = (sb->zsearch - sb->first_data_block)/(BLOCK_SIZE << 3);
	blk = firstblk;
	do
	{
		bit = bitmap_first_free(buffer_data(sb->zmap[blk]), BLOCK_SIZE);
		
		/* Found. */
		if (bit != BITMAP_FULL)
			goto found;
		
		/* Wrap around. */
		blk = (blk + 1 < sb->zmap_blocks) ? blk + 1 : 0;
	} while (blk != firstblk);
	
	return (BLOCK_NULL);

found:

	num =  sb->first_data_block + bit + blk*(BLOCK_SIZE << 3);
	
	/* 
	 * Remember disk block number to 
	 * speedup next block allocation.
	 */
	sb->zsearch = num;
	
	/* Allocate block. */
	bitmap_set(buffer_data(sb->zmap[blk]), bit);
	buffer_dirty(sb->zmap[blk], 1);
	sb->flags |= SUPERBLOCK_DIRTY;
	
	/* Clean block to avoid security issues. */
	buf = bread(sb->dev, blk);	
	kmemset(buffer_data(buf), 0, BLOCK_SIZE);
	buffer_dirty(buf, 1);
	brelse(buf);
	
	return (num);
}
Exemplo n.º 17
0
////////////////////////////////////////////////////
// 功能: 
// 输入: 
// 输出:
// 返回: 
////////////////////////////////////////////////////
HANDLE Mp3DecodeCreate()
{
	MP3_STREAM *stream;

	stream = kmalloc(sizeof(MP3_STREAM));
	if(stream == NULL)
		return NULL;
	kmemset(stream, 0x00, sizeof(MP3_STREAM));
	Mp3BufInit(&stream->Buffer);
	Mp3RequantizeInit(stream);
	Mp3SynthInit(stream);

	Mp3DecodeFirstFrame = 0;

	return stream;
}
Exemplo n.º 18
0
int kprintf(const char *fmt, ...)
{
    /* yes yes, a possible buffer overflow.. I know.
     * I'm to lazy to do something about it, atm */
    char buf[1024];
    va_list ap;
    int ret;

    kmemset(buf, 0, sizeof buf);

    va_start(ap, fmt);
    ret = kvsprintf(buf, fmt, ap);
    va_end(ap);

    kputs(buf);

    return ret;
}
Exemplo n.º 19
0
size_t rtl8139_send(struct network_dev *dev, uint8_t *_buf, size_t length)
{
	struct rtl8139 *rtl = dev->device;
	
	void* tx_buffer = (void *)(rtl->tx_buffers + 8192*rtl->tx_cur);
	kmemset(tx_buffer, 0, (length <60) ? 60 : length);
	kmemcpy(tx_buffer, _buf, length);
		
	if(length < 60)
		length = 60;
	
	rtl_outl(rtl, 0x20 + rtl->tx_cur*4, V2P(tx_buffer));
	rtl_outl(rtl, 0x10 + rtl->tx_cur*4, length | (48 << 16)); 
	rtl->tx_cur++;
	rtl->tx_cur %= 4;

	return length;
}
Exemplo n.º 20
0
////////////////////////////////////////////////////
// 功能: 
// 输入: 
// 输出:
// 返回: 
// 说明: 
////////////////////////////////////////////////////
static void DacDeviceOpen(void)
{
	int i;

	MediaDraveInit(1);

	// 这个函数初始化波特率与声道, 与pop noise没关系
	OpenMediaDevice(DAC_SAMPLE_RATIO,2,0);

	//初始化当前DMA状态为INIT状态
	fBeginDma = INIT_DMA_TRANS;

	//初始化结构体数据
	kmemset(&DacDevice,0,sizeof(RESAMPLE));

	//初始化缓冲
	for( i = 0 ; i < MAX_PCMBUFS ; i++ )
		DacDevice.BufFlag[i] = DAC_BUF_WRITE;
}
Exemplo n.º 21
0
void
descriptor_table_idt_init(void)
{
    uint32 index;

    idt_reg.limit = sizeof(idt_entry_list) - 1;
    idt_reg.base = (ptr_t)&idt_entry_list;

    kmemset(&idt_entry_list, 0, sizeof(idt_entry_list));
    pic_remap();

    index = 0;
    while (index < ARRAY_CNT_OF(isr_handler)) {
        descriptor_table_idt_entry_set(index, isr_handler[index],
            IDT_CODE_SEL, ATTR_INT_32);
        index++;
    }

    idt_table_flush((uint32)&idt_reg);

    printf_vga_ts("IDT table initialized.\n");
}
Exemplo n.º 22
0
void	create_gdt()
{
  gdt_ptr.limit = sizeof(t_gdt_entry) * MAX_DESCRIPTORS - 1;
  gdt_ptr.base = (u32)&gdt;

  kmemset(gdt, 0, sizeof(gdt));
  //Null Segment
  gdt_set_descriptor(0, 0, 0, 0, 0);
  //Code Segment
  gdt_set_descriptor(1, 0, 0xFFFFFFFF, 0x9A, 0xCF);
  //Data Segment
  gdt_set_descriptor(2, 0, 0xFFFFFFFF, 0x92, 0xCF);
  //User mode Code Segment
  gdt_set_descriptor(3, 0, 0xFFFFFFFF, 0xFA, 0xCF);
  //User mode Data Segment
  gdt_set_descriptor(4, 0, 0xFFFFFFFF, 0xF2, 0xCF);
  //TSS
  load_default_tss();

  gdt_flush((u32)&gdt_ptr);
  tss_flush();
}
Exemplo n.º 23
0
void rtl8139_start(struct rtl8139 *rtl)
{
	rtl->tx_cur = 0;
	rtl_outb(rtl, 0x37, 0x10);
	while((rtl_inb(rtl, 0x37) & 0x10) != 0);
	
	kmemset(rtl->rx_buffer, 0, (8192*8)+16+1500);
	rtl_outl(rtl, 0x30,(uintptr_t)V2P(rtl->rx_buffer));
	rtl_outb(rtl, 0x37, 0xc);
	
	for(int i=0; i < 4; i++)
	{
		rtl_outl(rtl, 0x20 + i*4, (uintptr_t)V2P(rtl->tx_buffers) + i*(8192 +16+1500));
	}
	//TODO: need to register pci IRQs instead of doing it directly
	//interrupt_register(32 + rtl->pci_hdr->int_line, &rtl_handler);

	pci_register_irq(rtl->pci, &rtl_handler, rtl);
	rtl_outl(rtl, 0x44, (1 << 7) | 8|  (1 << 1));
	rtl_outw(rtl, 0x3c, 0x5 );
	for(int i = 0; i < 6; i ++)
		rtl->mac[i] = rtl_inb(rtl, i);
	for(int i = 0; i < 100; i++)
	{
	/*	uint16_t isr = rtl_inw(rtl, ISR);
		if(isr & 0x20)
		{
			rtl_outw(rtl, ISR, 0x20);
			printf("isr %x\n",isr);
			break;
		}*/
	}
	
	



}
Exemplo n.º 24
0
PageStruct* allocate_page(){

	/*
	Thus function pulls the first available page from the page free list and returns a 
	virtual addresss corresponding to that free page
	*/
	PageStruct* pageToReturn = page_free_list;

	if(pageToReturn){

//	printf("Allocating page: %p\t", pageToPhysicalAddress(pageToReturn));
        free_pages--;
		page_free_list = page_free_list->next;

		pageToReturn->next = NULL;
		kmemset((uint64_t*)KADDR(pageToPhysicalAddress(pageToReturn)),0,PGSIZE);
	}
	else{
		printf("ERROR!!! No pages to allocate in the free list\n");
    while(1);
    }
	return pageToReturn;
}
Exemplo n.º 25
0
void initialize_vm_64(void){
	uint64_t* pml4e;
    pages = boot_alloc(npages*sizeof(struct PageStruct));
    procs = boot_alloc(NPROCS*sizeof(struct ProcStruct)); 
    proc_free_list = procs;

	pml4e = boot_alloc(PGSIZE);
	boot_pml4e = pml4e;
	boot_cr3 = (physaddr_t*)PADDR(pml4e);
	kmemset(boot_pml4e,0,PGSIZE);
         initialize_page_lists();
    printf("Total free=%d",free_pages); 

  //    while(i--);
   if(  map_vm_pm(boot_pml4e, (uint64_t)PHYSBASE,PADDR(PHYSBASE),(uint64_t)(boot_alloc(0)-PHYSBASE),PTE_P|PTE_W)==-1)
       while(1);
   if(  map_vm_pm(boot_pml4e, (uint64_t)KERNBASE+PGSIZE,0x1000,0x7000000-0x1000,PTE_P|PTE_W)==-1)//KERNELSTACK =tss.rspp0       
   while(1);
   if( map_vm_pm(boot_pml4e, (uint64_t)VIDEO_START,PADDR(VIDEO_START),10*0x1000,PTE_P|PTE_W)==-1)
    while(1);
    printf("TotalFREE=%d",free_pages);
   
     lcr3(boot_cr3);
}
Exemplo n.º 26
0
void debugger_getprocs_callback(struct allocation_t* ref)
{
	if (!ref)
		return;

	struct message_t* message = __get(ref);
	if (!message)
		return;

	// Only handle requests
	if (message->header.request != 1)
		goto cleanup;

	int(*_sx_slock)(struct sx *sx, int opts, const char *file, int line) = kdlsym(_sx_slock);
	void(*_sx_sunlock)(struct sx *sx, const char *file, int line) = kdlsym(_sx_sunlock);
	void(*_mtx_lock_flags)(struct mtx *m, int opts, const char *file, int line) = kdlsym(_mtx_lock_flags);
	void(*_mtx_unlock_flags)(struct mtx *m, int opts, const char *file, int line) = kdlsym(_mtx_unlock_flags);
	struct sx* allproclock = (struct sx*)kdlsym(allproc_lock);
	struct proclist* allproc = (struct proclist*)*(uint64_t*)kdlsym(allproc);

	void(*vmspace_free)(struct vmspace *) = kdlsym(vmspace_free);
	struct vmspace* (*vmspace_acquire_ref)(struct proc *) = kdlsym(vmspace_acquire_ref);
	void(*_vm_map_lock_read)(vm_map_t map, const char *file, int line) = kdlsym(_vm_map_lock_read);
	void(*_vm_map_unlock_read)(vm_map_t map, const char *file, int line) = kdlsym(_vm_map_unlock_read);

	uint64_t procCount = 0;
	struct proc* p = NULL;
	struct debugger_getprocs_t getproc = { 0 };

	sx_slock(allproclock);
	FOREACH_PROC_IN_SYSTEM(p)
	{
		PROC_LOCK(p);
		// Zero out our process information
		kmemset(&getproc, 0, sizeof(getproc));

		// Get the vm map
		struct vmspace* vm = vmspace_acquire_ref(p);
		vm_map_t map = &p->p_vmspace->vm_map;
		vm_map_lock_read(map);

		struct vm_map_entry* entry = map->header.next;

		// Copy over all of the address information
		getproc.process_id = p->p_pid;
		getproc.text_address = (uint64_t)entry->start;
		getproc.text_size = (uint64_t)entry->end - entry->start;
		getproc.data_address = (uint64_t)p->p_vmspace->vm_daddr;
		getproc.data_size = p->p_vmspace->vm_dsize;
		// Copy over the name and path
		kmemcpy(getproc.process_name, p->p_comm, sizeof(getproc.process_name));
		kmemcpy(getproc.path, p->p_elfpath, sizeof(getproc.path));
		// Write it back to the PC
		kwrite(message->socket, &getproc, sizeof(getproc));
		procCount++;

		// Free the vmmap
		vm_map_unlock_read(map);
		vmspace_free(vm);

		PROC_UNLOCK(p);
	}
	sx_sunlock(allproclock);
	// Send finalizer, because f**k this shit
	kmemset(&getproc, 0xDD, sizeof(getproc));
	kwrite(message->socket, &getproc, sizeof(getproc));

cleanup:
	__dec(ref);
}
Exemplo n.º 27
0
void debugger_readmem_callback(struct allocation_t* ref)
{
	struct message_t* message = __get(ref);
	if (!message)
		return;

	if (message->header.request != 1)
		goto cleanup;

	if (message->socket < 0)
		goto cleanup;

	if (!message->payload)
		goto cleanup;

	struct debugger_readmem_t* request = (struct debugger_readmem_t*)message->payload;
	if (request->process_id < 0)
	{
		WriteLog(LL_Error, "invalid process id.");
		goto error;
	}

	if (request->address == 0)
	{
		WriteLog(LL_Error, "Invalid address");
		goto error;
	}

	if (request->dataLength == 0)
	{
		WriteLog(LL_Error, "Invalid data length.");
		goto error;
	}

	void(*_mtx_unlock_flags)(struct mtx *m, int opts, const char *file, int line) = kdlsym(_mtx_unlock_flags);
	struct  proc* (*pfind)(pid_t) = kdlsym(pfind);

	struct proc* process = pfind(request->process_id);
	if (process == 0)
		goto error;

	int result = proc_rw_mem(process, (void*)request->address, request->dataLength, request->data, &request->dataLength, 0);
	PROC_UNLOCK(process);

	WriteLog(LL_Debug, "proc_rw_mem returned(%d, %p, %d, %p, %d, %s) %d", process, request->address, request->dataLength, request->data, &request->dataLength, "read", result);
	if (result < 0)
		goto error;

	message->header.request = 0;
	kwrite(message->socket, request, sizeof(*request));

	// Error conditions
	if (1 == 0)
	{
	error:
		kmemset(request, 0, sizeof(*request));
		request->process_id = -1;
		kwrite(message->socket, request, sizeof(*request));
	}

cleanup:
	__dec(ref);
}
Exemplo n.º 28
0
////////////////////////////////////////////////////
// 功能: 
// 输入: 
// 输出:
// 返回: 
// 说明: 
////////////////////////////////////////////////////
static void JzThreadRecordFile(DWORD p)
{
	PAK_OBJECT obj;
	PAK_VFILE vfile;
	PMEDIA_TASK task;
	MESSAGE msg;
	int recsize;
	int samp_size;
	void *buf_in;
	BYTE *fade_buf;
	int msgid;
	int wsize,samp_time;
	int obj_del = 1;
	int fade_len;
	int fade_record = RECORD_FADE;

	// 获取打开信息
	obj = (PAK_OBJECT)p;
	task = &obj->Task;
	vfile = &obj->File;
	if(!task->AChannels)
		task->AChannels = 1;
	if(!task->ASamplerate)
		task->ASamplerate = 8000;
	
	// 写入头信息
	samp_size = 8192 * 2;
	msgid = 0;
	WaveFileHead.bytes_per_samp = task->AChannels * 2;
	WaveFileHead.bytes_per_sec = WaveFileHead.bytes_per_samp * task->ASamplerate;
	WaveFileHead.num_chans = task->AChannels;
	WaveFileHead.sample_rate = task->ASamplerate;
	if( task->MediaType == AK_REC_TYPE_WAVE)
	{
		if(JzCbFileWrite(&WaveFileHead, sizeof(WAVE_HEADER),1,vfile) != sizeof(WAVE_HEADER))
		{
			*obj->bReady = -1;
			goto ThreadEnd;
		}
	}

	// 打开音频编码库
	buf_in = kmalloc(samp_size);
	if(buf_in == NULL)
	{
		*obj->bReady = -1;
		goto ThreadEnd;
	}

	// 打开ADC设备
	obj->hDac = AdcOpen(task->ASamplerate, task->AChannels);
	if(obj->hDac == NULL)
	{
		kdebug(mod_audio, PRINT_ERROR, "ADC device open failed! CH: %d, %dHz\n", task->AChannels, task->ASamplerate);
		kfree(buf_in);
		*obj->bReady = -1;
		goto ThreadEnd;
	}
	AdcSetVolume(obj->hDac, obj->Task.Volume);
	AdcSetEq(obj->hDac, obj->Task.Eq);

	// 发送WM_MEDIA_OPEN消息
	if(task->hWnd)
	{
		msg.hWnd = task->hWnd;
		msg.Data.v = 0;
		msg.hWndSrc = 0;
		msg.MsgId = WM_MEDIA_OPEN;
		msg.Param = 0;
	#if defined(STC_EXP)
		sGuiMsgPutMsg(&msg);
	#else
		GuiMsgPutMsg(&msg);
	#endif
	}
	
	// 等待条件,写入数据
	*obj->bReady = 1;
	recsize = 0;
	fRecodeWav = 1;
	while(!obj->bClose && !obj->bTerminate)
	{
		int i;
		
		// 从DA读取PCM数据
		for(i=0; i<8; i++)
		{
			JzSrvProcMsg(obj);
			wsize = AdcRead(obj->hDac, (short*)((DWORD)buf_in+(i*samp_size)/8), samp_size/8);
			samp_time = (samp_size * 1000 / 8) / (task->ASamplerate * task->AChannels * sizeof(short)) ;

			if( fade_record )
			{	//fade时,不进行时间计算,递减fade时间
				if( fade_record > samp_time )
					fade_record -= samp_time;
				else
					fade_record = 0;
			}
			else	//fade结束,可以进行时间计算
				obj->RecDuration += samp_time;
		}

		if( !fade_record )
			recsize += samp_size;
		
		// 检查录音时间是否结束
		if(obj->Task.Duration && (obj->RecDuration >= obj->Task.Duration))
		{
			recsize -= samp_size;
			obj->bClose = 1;
			break;
		}

		// 直接把PCM数据写入文件
		if( !fade_record )
		{
			wsize = JzCbFileWrite(buf_in, samp_size,1,vfile);
			if(wsize != samp_size)
			{
				if(wsize > 0)
					recsize -= samp_size - wsize;
				else
					recsize -= samp_size;
				obj->bClose = 2;
				break;
			}
		}

		// 处理控制消息
		JzSrvProcMsg(obj);
		while(obj->bRecPause && !obj->bClose && !obj->bTerminate)
		{
			JzSrvProcMsg(obj);
			sTimerSleep(100, NULL);
		}
	}

	//fade时,不保存文件
	if( !fade_record )
	{
		fade_len = (task->ASamplerate * task->AChannels * sizeof(short) * RECODE_SKIP_LEN) / 1000;
		if( task->MediaType == AK_REC_TYPE_WAVE)
		{
			// 录音结束,写入头/尾信息
			JzCbFileSeek(vfile, 0, SEEK_SET);
			WaveFileHead.file_size = recsize + sizeof(WaveFileHead) - 8;
			WaveFileHead.data_length = recsize;
			JzCbFileWrite(&WaveFileHead, sizeof(WAVE_HEADER),1,vfile);

			if( vfile->PlayLength > (fade_len + sizeof(WaveFileHead)) && fade_len )
			{
				fade_buf = (BYTE*)kmalloc(fade_len);
				kmemset(fade_buf,0,fade_len);
				JzCbFileSeek(vfile, recsize + sizeof(WaveFileHead) - fade_len , SEEK_SET);
				JzCbFileWrite(fade_buf, fade_len,1,vfile);
				kfree(fade_buf);
				kdebug(mod_audio, PRINT_INFO, "recode fade: skip wav len = %d\n",fade_len);
			}
		}
		else
		{
			if( vfile->PlayLength > fade_len && fade_len )
			{
				fade_buf = (BYTE*)kmalloc(fade_len);
				kmemset(fade_buf,0,fade_len);
				JzCbFileSeek(vfile, recsize - fade_len, SEEK_SET);
				JzCbFileWrite(fade_buf, fade_len,1,vfile);
				kfree(fade_buf);
				kdebug(mod_audio, PRINT_INFO, "recode fade: skip pcm len = %d\n",fade_len);
			}
		}

	}
	JzCbFileWriteFlush(vfile);
	fRecodeWav = 0;
	
	// 线程结束, 关闭文件和设备
	AdcClose(obj->hDac);

	// 设置线程结束标志	
	if(obj->bTerminate || (obj->bClose == 2))
	{	
		msgid = WM_MEDIA_TERMINATE;
		msg.MsgId = msgid;
		msg.hWnd = task->hWnd;
	}
	else if(obj->bClose)
	{	
		msgid = WM_MEDIA_CLOSE;
		msg.MsgId = msgid;
		msg.hWnd = task->hWnd;
	}
	else
	{
		// 非正常结束
		msgid = 0;
		*obj->bReady = -1;
	}
	
	// 设置返回消息参数	
	if(obj->bTerminate == 2)
		msg.Data.v = 1;
	else if(obj->bClose == 2)
		msg.Data.v = 2;
	else
		msg.Data.v = 0;
	
	// 删除对象
	if(obj->bTerminate == 1)
		obj_del = 0;
	else
		JzSrvDestroyNotify(task);
	
	// 发送WM_MEDIA_CLOSE消息
	if(msgid)
	{
		msg.hWndSrc = 0;
		msg.Param = 0;
	#if defined(STC_EXP)
		sGuiMsgPutMsg(&msg);
	#else
		GuiMsgPutMsg(&msg);
	#endif
	}

	if(buf_in)
		kfree(buf_in);
ThreadEnd:	
	// 设置线程结束标志	
	if(!obj_del)
		obj->bTerminate++;

#if defined(STC_EXP)
	sThreadTerminate(sThreadSelf());
#else
	ThreadTerminate(ThreadSelf());
#endif
}
Exemplo n.º 29
0
void
kinitlong(unsigned long pmemsz)
{
#if (NEWTMR)
    uint32_t tmrcnt = 0;
#endif

/* initialise interrupt management */
#if (VBE)
    trapinitprot();
#endif
    /* initialise virtual memory */
    vminitlong((uint64_t *)kernpagemapl4tab);
#if 0
    /* FIXME: map possible device memory */
    vmmapseg((uint32_t *)&_pagetab, DEVMEMBASE, DEVMEMBASE, 0xffffffffU,
             PAGEPRES | PAGEWRITE | PAGENOCACHE);
#endif
//    schedinit();
    /* zero kernel BSS segment */
    kbzero(&_bssvirt, (uint32_t)&_ebssvirt - (uint32_t)&_bssvirt);
    /* set kernel I/O permission bitmap to all 1-bits */
    kmemset(&kerniomap, 0xff, sizeof(kerniomap));
    /* INITIALIZE CONSOLES AND SCREEN */
#if (VBE)
    vbeinitscr();
#endif
#if (VBE) && (NEWFONT)
    consinit(768 / vbefontw, 1024 / vbefonth);
#elif (VBE)
    consinit(768 >> 3, 1024 >> 3);
#endif
    /* TODO: use memory map from GRUB? */
//    vminitphys((uintptr_t)&_epagetab, pmemsz);
    vminitphys((uintptr_t)&_epagetab, pmemsz);
    meminit(pmemsz);
    tssinit(0);
#if (VBE) && (NEWFONT)
//    consinit(768 / vbefontw, 1024 / vbefonth);
#elif (VBE)
    consinit(768 >> 3, 1024 >> 3);
#endif
#if (SMBIOS)
    smbiosinit();
#endif
#if (PS2DRV)
    ps2init();
#endif
#if (VBE) && (PLASMA)
    plasmaloop();
#endif
#if (VBE)
    vbeprintinfo();
#endif
    logoprint();
//    vminitphys((uintptr_t)&_ebss, pmemsz - (unsigned long)&_ebss);
    /* HID devices */
#if (PCI)
    /* initialise PCI bus driver */
    pciinit();
#endif
#if (ATA)
    /* initialise ATA driver */
    atainit();
#endif
#if (SB16)
    /* initialise Soundblaster 16 driver */
    sb16init();
#endif
#if (ACPI)
    /* initialise ACPI subsystem */
    acpiinit();
#endif
    /* initialise block I/O buffer cache */
    if (!bufinit()) {
        kprintf("failed to allocate buffer cache\n");

        while (1) {
            ;
        }
    }
    /* allocate unused device regions (in 3.5G..4G) */
//    pageaddzone(DEVMEMBASE, &vmshmq, 0xffffffffU - DEVMEMBASE + 1);
#if (SMP) || (APIC)
//#if (SMP)
    /* multiprocessor initialisation */
//    mpinit();
//#endif
    if (mpncpu == 1) {
        kprintf("found %ld processor\n", mpncpu);
    } else {
        kprintf("found %ld processors\n", mpncpu);
    }
#if (HPET)
    /* initialise high precision event timers */
    hpetinit();
#endif
#if (NEWTMR)
    tmrcnt = apicinitcpu(0);
#else
    apicinitcpu(0);
#endif
#if (IOAPIC)
    ioapicinit(0);
#endif
#endif /* SMP || APIC */
#if (SMP)
    if (mpmultiproc) {
        mpstart();
    }
#endif
    /* CPU interface */
    taskinit();
//    tssinit(0);
//    machinit();
    /* execution environment */
    procinit(PROCKERN);
//    k_curtask = &k_curproc->task;
//    sysinit();
    kprintf("DMA buffers (%ul x %ul kilobytes) @ 0x%p\n",
            DMANCHAN, DMACHANBUFSIZE >> 10, DMABUFBASE);
    kprintf("VM page tables @ 0x%p\n", (unsigned long)&_pagetab);
//    kprintf("%ld kilobytes physical memory\n", pmemsz >> 10);
    kprintf("%ld kilobytes kernel memory\n", (uint32_t)&_ebss >> 10);
    kprintf("%ld kilobytes allocated physical memory (%ld wired, %ld total)\n",
            (vmpagestat.nwired + vmpagestat.nmapped + vmpagestat.nbuf) << (PAGESIZELOG2 - 10),
            vmpagestat.nwired << (PAGESIZELOG2 - 10),
            vmpagestat.nphys << (PAGESIZELOG2 - 10));
    k_curcpu = &cputab[0];
    cpuinit(k_curcpu);
    schedinit();
#if (APIC)
    apicstarttmr(tmrcnt);
#else
    pitinit();
#endif
    schedloop();

    /* NOTREACHED */
}
Exemplo n.º 30
0
////////////////////////////////////////////////////
// 功能: 
// 输入: 
// 输出:
// 返回: 
// 说明: 
////////////////////////////////////////////////////
HANDLE DacOpen(void)
{
	PDAC_DEVICE dac;
	PLIST head;
	PLIST n;
	int open_devs;
	int i;
 
	// 获取打开设备数量
	kMutexWait(hDacMutex);
	head = &DacList;
	open_devs = 0;
	for(n=ListFirst(head); n!=head; n=ListNext(n))
		open_devs++;

	if(open_devs == 4)
	{
		kdebug(mod_audio, PRINT_WARNING, "no dac for open\n");
		kMutexRelease(hDacMutex);
		return NULL;
	}

	dac = kmalloc(sizeof(DAC_DEVICE));
	if(dac == NULL)
	{
		kdebug(mod_audio, PRINT_ERROR, "open dac malloc failed\n");
		kMutexRelease(hDacMutex);
		return NULL;
	}
	// 初始化设备对象
	kmemset(dac, 0x00, sizeof(DAC_DEVICE));

	dac->Channel  = 0;
	dac->Samprate = 0;
	dac->Volume   = 0;
	dac->hWsola   = NULL;
	dac->WsolaBuf = NULL;
	dac->pSamplerate = NULL;
	for(i=0; i<MAX_PCMBUFS; i++)
		dac->Resample.BufFlag[i] = DAC_BUF_WRITE;
	dac->Resample.WriteOffset = 0;
	dac->Resample.ReadBuf = 0;
	dac->Resample.WriteBuf = 0;
	nErrorData = 0;
	ListInit(&dac->Link);

#ifdef DAC_SAVE_PCM
	dac_offset = 0;
	dac_source_offset = 0;
#endif
	// 检查当前设备是否已经打开
	if(ListEmpty(&DacList))
	{
		// 本来打开功放, mos管和开dac的工作应该在ListEmpty()==true里面进行.
		// 但是当打开两路声音, 而且其中一路在暂停状态, 因为暂停的时候关闭了功放和dac,
		// 所以需要再次打开功放和dac, 于是改成不在判断语句内打开, 对于多个声音合成,
		// 相当于多打开一次dac和功放, 应该没有影响.
#if defined(CONFIG_MAC_NP7000) || defined(CONFIG_MAC_NP2300)
		MillinsecoundDelay(250);

		SetMoseCe(1);
#else
#if !defined(CONFIG_MAC_NP5800) && !defined(CONFIG_MAC_NP6800)
		SetPowerAmplifier(1);
#endif
#endif
		// 打开DA设备
		DacDeviceOpen();

		// 打开耳机设备
		DacHeadphoneOpen();

		//开启DMA数据传送,同时只传送静音数据
		StartDmaPcmTrans(1,0);

		// 释放设备打开信号
		kSemaRelease(hDacSema);

		ListInsert(&DacList, &dac->Link);

		//只有第一次打开声音设备才需要去掉BOBO音
#if defined(CONFIG_MAC_NP7000) || defined(CONFIG_MAC_NP2300)
		//MillinsecoundDelay(250);
		sTimerSleep(250, 0);
		SetPowerAmplifier(1);
#else
		//MillinsecoundDelay(120);
		sTimerSleep(120, 0);

		SetMoseCe(1);

#if defined(CONFIG_MAC_NP5800) || defined(CONFIG_MAC_NP6800)
		//MillinsecoundDelay(100);
		sTimerSleep(100, 0);
		SetPowerAmplifier(1);
		//MillinsecoundDelay(30);
		sTimerSleep(30, 0);
#else
		MillinsecoundDelay(130);
#endif
		SetMuteMode(1);
#endif
	}
	else
	{
		//防止一个声音暂停时,另外一个声音无法播放的问题
		SetMuteMode(1);
		ListInsert(&DacList, &dac->Link);
	}


	kMutexRelease(hDacMutex);
	return (HANDLE)dac;
}