Ejemplo n.º 1
0
int _start(void)
{
	enableMMU();
	vectorCopy();//拷贝中断向量表

	enableInterruptCPSR();

	enableExt(64, 26,4, 0);

	return 0;
}
int main()
{
	if(enableMMU()!=0)
		return 1;
	auto tcr = RegTCR_EL1::read();
	auto ttbr1Mask = upperMaskBits(tcr.T1SZ);

	// 将sp, x29(base pointer) 的高位置为1
	__asm__ __volatile__(
			"mov x0,sp \n\t"
			"orr x0,x0,%0 \n\t"
			"mov sp,x0 \n\t"
			"orr x29,x29, %0 \n\t"::"r"(ttbr1Mask):"sp","x0");

	// 跳转到高地址处
	extern char mainStart[];
	RegPC pc;
	pc.PC = reinterpret_cast<uint64_t>(mainStart) | ttbr1Mask;
	pc.write();
	ASM_DEFINE_LOCAL_SYM(mainStart);

	// 重新初始化内存管理器
	extern char ramStart[];
	extern char ramEnd[];
	size_t ramSize = static_cast<size_t>(ramEnd - ramStart);
	new (&mman) MemoryManager( reinterpret_cast<void*>(reinterpret_cast<uint64_t>(ramStart)|ttbr1Mask), ramSize,true );

	// 重新初始化pidManager
	new (&pidManager) PidManager();
	new (&processManager) ProcessManager();
	new (&systemFeatures) SystemFeatures();

	new (&pl011) PL011(reinterpret_cast<volatile void*>(UART_BASE|ttbr1Mask));
	new (&kin)  Input();
	new (&kout) Output();

//	kout << "&mman = " << &mman << "\n";


	systemFeatures.updatePreconfigured();
	systemFeatures.cores(4);
	systemFeatures.architecture(Architecture::AARCH64);
	systemFeatures.asidSelector(0);

	// my tests here
//	// qemu 在从终端输入时,总是buffered模式,只有回车之后才会将数据写入到串口的缓冲区里,因此只要数据是一次读完就能成功读取
//	char ch=0;
//	while( (ch=kin.getchar())!='\n')
//		kout << ch;
//	kout << "\n";
//	kout << "echo end\n";
//
//	asm_wfe_loop();

//	RAMFile file("file1");
//	kout << "diskSize = " << file.diskSize() << "\n";
//	file.emplaceAppend("what is the f**k");
//	kout << "content = " << file.content() << "\n";
//	kout << "now diskSize = " << file.diskSize() << "\n";
//	asm_wfe_loop();



	// 建立一个进程
	auto processLink = processManager.createNewProcess(
			64 - tcr.T0SZ, // virtual address length in bits
			nullptr,  // parent
			10, // Priority
			USER_SPACE_SIZE, // codeSize
			Process::PAGE_SIZE * Process::HEAP_L3_ENTRY_NUM, // Heap Size
			Process::PAGE_SIZE * Process::STACK_L3_ENTRY_NUM  // Stack Size
			);
	Process & process = processLink->data<true>();
	if(processLink==nullptr ||
			process.status()==Process::CREATED_INCOMPLETE)
	{
		kout << FATAL << "create process failed\n";
		return 1;
	}

	// 复制代码到分配给进程的代码段空间
	const void *userSpaceStart = reinterpret_cast<const void*>(USER_SPACE_START | ttbr1Mask);
	std::memcpy(process.codeBase(), userSpaceStart, USER_SPACE_SIZE);

	process.registers()[30] = process.ELR().returnAddr;

	// 使用任务调度切换到下一个进程
	processManager.changeProcessStatus(processLink, Process::RUNNING);


	void *spEL1=reinterpret_cast<void*>(reinterpret_cast<uint64_t>(__stack_top)|ttbr1Mask);
	process.restoreContextAndExecute(spEL1);

	return 0;
}