/* Gestiona la copia del cbuffer i els problemes ocasionats al ser un * procés diferent del bloquejat el que ha d'accedir al espai d'@ del * procés bloquejat. * Llegeix min(keystoread,size del cbuffer) elements. * - Retorna 1: * Si ha pogut realitzar la copia. * - Retorna 0: * Si no ha pogut realitzar la copia per falta de pagines * lliures en el procés per poder accedir al espai d'@ del * procés bloquejat. * */ int keyboard_cbuffer_read() { struct list_head *list_blocked = list_first(&keyboardqueue); struct task_struct * blocked_pcb = list_head_to_task_struct(list_blocked); struct task_struct * current_pcb = current(); page_table_entry * pt_blocked = get_PT(blocked_pcb); page_table_entry * pt_current = get_PT(current_pcb); page_table_entry * dir_blocked = get_DIR(blocked_pcb); page_table_entry * dir_current = get_DIR(current_pcb); char bread; if (dir_blocked != dir_current) { // Si són 2 procesos independents int id_pag_buffer = ((int)blocked_pcb->kbinfo.keybuffer&0x003ff000)>>12; int addr_buffer = ((int)blocked_pcb->kbinfo.keybuffer&0x00000FFF); /*Cerca de entradeslliures a la taula de pàgines */ int free_pag = FIRST_FREE_PAG_P; while(pt_current[free_pag].entry != 0 && free_pag<TOTAL_PAGES) free_pag++; if (free_pag == TOTAL_PAGES) return 0; // Cas d'error set_ss_pag(pt_current,free_pag, pt_blocked[id_pag_buffer].bits.pbase_addr); while (!circularbIsEmpty(&cbuffer) && blocked_pcb->kbinfo.keystoread > 0) { circularbRead(&cbuffer,&bread); copy_to_user(&bread, (void *)((free_pag<<12)+addr_buffer), 1); blocked_pcb->kbinfo.keystoread--; blocked_pcb->kbinfo.keysread++; blocked_pcb->kbinfo.keybuffer++; addr_buffer++; /* Si s'ha de canviar la pàgina */ if (addr_buffer == PAGE_SIZE) { id_pag_buffer++; set_ss_pag(pt_current,free_pag, pt_blocked[id_pag_buffer].bits.pbase_addr); set_cr3(dir_current); } } del_ss_pag(pt_current, free_pag); }
int sys_fork() { update_user_to_system(); int PID=-1; int i; int j; // creates the child process /* a) Get a free task_struct for the process. If there is no space for a new process, an error will be returned. b) Inherit system data: copy the parent’s task_union to the child. Determine whether it is necessary to modify the page table of the parent to access the child’s system data. The copy_data function can be used to copy. c) Initialize field dir_pages_baseAddr with a new directory to store the process address space using the allocate_DIR routine. d) Search physical pages in which to map logical pages for data+stack of the child process (using the alloc_frames function). If there is no enough free pages, an error will be return. */ //a if(list_empty( &freequeue )){ update_system_to_user(current()); return -ENOMEM; } struct list_head * freequeue_head = list_first( &freequeue ); struct task_struct * child_struct = list_head_to_task_struct(freequeue_head); list_del(freequeue_head); // not in freequeue anymore //b struct task_struct * current_struct = current(); union task_union * current_union = (union task_union *) current_struct; union task_union * child_union = (union task_union *) child_struct; copy_data(current_union, child_union, sizeof(union task_union)); // TODO determine whether it is necessary to modify the page table of the parent to access the child’s system data //c allocate_DIR(child_struct); //d int physical_pages[NUM_PAG_DATA]; for(i = 0; i < NUM_PAG_DATA; ++i) { physical_pages[i] = alloc_frame(); if( physical_pages[i] < 0){ for(j = i-1; j >= 0; j--) { free_frame((unsigned int)j); } update_system_to_user(current()); return -EAGAIN; } } /* e) Inherit user data: i) Create new address space: Access page table of the child process through the direc- tory field in the task_struct to initialize it (get_PT routine can be used): A) Page table entries for the system code and data and for the user code can be a copy of the page table entries of the parent process (they will be shared) */ page_table_entry * child_pt = get_PT(child_struct); page_table_entry * parent_pt = get_PT(current_struct); int child_logical_address; for(child_logical_address = 0; child_logical_address < NUM_PAG_KERNEL + NUM_PAG_CODE; child_logical_address++) { int physical_parent_frame = get_frame(parent_pt, child_logical_address); set_ss_pag( child_pt, child_logical_address, physical_parent_frame); } /* B) Page table entries for the user data+stack have to point to new allocated pages which hold this region*/ for(; child_logical_address < NUM_PAG_KERNEL + NUM_PAG_CODE + NUM_PAG_DATA; child_logical_address++) { set_ss_pag(child_pt, child_logical_address, physical_pages[child_logical_address - (NUM_PAG_KERNEL + NUM_PAG_CODE)]); } /* ii) Copy the user data+stack pages from the parent process to the child process. The child’s physical pages cannot be directly accessed because they are not mapped in the parent’s page table. In addition, they cannot be mapped directly because the logical parent process pages are the same. They must therefore be mapped in new entries of the page table temporally (only for the copy). Thus, both pages can be accessed simultaneously as follows: A) Use temporal free entries on the page table of the parent. Use the set_ss_pag and del_ss_pag functions. B) Copy data+stack pages. C) Free temporal entries in the page table and flush the TLB to really disable the parent process to access the child pages. */ // direccion logica del systemcode+systemdata+usercode = direccion logica datos user int parent_log = NUM_PAG_KERNEL + NUM_PAG_CODE; for(i = 0; i < NUM_PAG_DATA; ++i){ set_ss_pag(parent_pt, parent_log + NUM_PAG_DATA + i, physical_pages[i]); copy_data( (void*) ((parent_log + i) * PAGE_SIZE), (void*) ((parent_log + NUM_PAG_DATA + i) * PAGE_SIZE), PAGE_SIZE ); del_ss_pag(parent_pt, parent_log + NUM_PAG_DATA + i); } //set_cr3(parent_pt); // flush tlb fallaba, por que? TODO set_cr3(get_DIR(current_struct)); /* f) Assign a new PID to the process. The PID must be different from its position in the task_array table. g) Initialize the fields of the task_struct that are not common to the child. i) Think about the register or registers that will not be common in the returning of the child process and modify its content in the system stack so that each one receive its values when the context is restored. h) Prepare the child stack emulating a call to context_switch and be able to restore its context in a known position. The stack of the new process must be forged so it can be restored at some point in the future by a task_switch. In fact the new process has to restore its hardware context and continue the execution of the user process, so you can create a routine ret_from_fork which does exactly this. And use it as the restore point like in the idle process initialization 4.4. i) Insert the new process into the ready list: readyqueue. This list will contain all processes that are ready to execute but there is no processor to run them. j) Return the pid of the child process. */ if(_PID == INT_MAX){ update_system_to_user(current()); return -1; // please restart } child_struct->PID = _PID++; PID = child_struct->PID; /* * 0 -19 * ret_from_fork -18 * sys_call_handler // -17 * SAVEALL // -16 * iret // -5 */ child_struct->kernel_esp = (unsigned long *)&child_union->stack[KERNEL_STACK_SIZE-19]; child_union->stack[KERNEL_STACK_SIZE-19] = 0; child_union->stack[KERNEL_STACK_SIZE-18] = (int)ret_from_fork; list_add_tail(&child_struct->list, &readyqueue); child_struct->stats.elapsed_total_ticks = get_ticks(); child_struct->stats.user_ticks = 0; child_struct->stats.system_ticks = 0; child_struct->stats.blocked_ticks = 0; child_struct->stats.ready_ticks = 0; child_struct->stats.total_trans = 0; child_struct->stats.remaining_ticks = 0; // last_forked_child = child_struct = 0; update_system_to_user(current()); return PID; }