int register_mce_handler(struct ras_events *ras, unsigned ncpus) { int rc; struct mce_priv *mce; ras->mce_priv = calloc(1, sizeof(struct mce_priv)); if (!ras->mce_priv) { log(ALL, LOG_INFO, "Can't allocate memory MCE data\n"); return ENOMEM; } mce = ras->mce_priv; rc = detect_cpu(ras); if (rc) { if (mce->processor_flags) free (mce->processor_flags); free (ras->mce_priv); ras->mce_priv = NULL; return (rc); } switch (mce->cputype) { case CPU_SANDY_BRIDGE_EP: case CPU_IVY_BRIDGE_EPEX: case CPU_HASWELL_EPEX: case CPU_KNIGHTS_LANDING: case CPU_KNIGHTS_MILL: set_intel_imc_log(mce->cputype, ncpus); default: break; } return rc; }
int main() { int cpu_model = detect_cpu(); if (cpu_model < 0) { printf("Unsupported CPU type\n"); return -1; } int core0 = msr_open(0); int core1 = msr_open(1); PETU_t pet0 = msr_calculate_units(core0); PETU_t pet1 = msr_calculate_units(core1); PackagePowerInfo_t ppi0 = msr_get_package_power_info(core0, &pet0); PackagePowerInfo_t ppi1 = msr_get_package_power_info(core1, &pet1); printf("PP Core 0: %.3f %.3f\n", ppi0.minimum_power, ppi0.maximum_power); printf("PP Core 1: %.3f %.3f\n", ppi1.minimum_power, ppi1.maximum_power); printf("Computation begins\n"); double e0 = msr_get_package_energy(core0, &pet0); double e1 = msr_get_package_energy(core1, &pet1); computation(); e0 = msr_get_package_energy(core0, &pet0) - e0; e1 = msr_get_package_energy(core1, &pet1) - e1; printf("Computation ends\n"); printf("Energy consumed: Core 0: %.3fJ\n", e0); printf("Energy consumed: Core 1: %.3fJ\n", e1); return 0; }
void kmain(struct multiboot_info *mboot_ptr, uint32_t initial_stack) { init_console(); kprintf("Booting garethix, SVN version %s...\n", KERNEL_SVN_REV); kprintf("Build: %s, Build host: %s\n", BUILDTIME, BUILDHOST); kprintf("command line: %s\n",(char*)mboot_ptr->cmdline); detect_cpu(); kprintf("Kernel initrd should be at 0x%x\n", mboot_ptr->mods_addr); kprintf("Dumping memory map:\n"); memory_map_t* mmap = mboot_ptr->mmap_addr; uint32_t largest_mem=0; uint32_t largest_mem_size=0; while(mmap < mboot_ptr->mmap_addr + mboot_ptr->mmap_length) { if (mmap->length_low > largest_mem_size) { largest_mem = mmap->base_addr_low; largest_mem_size = mmap->length_low; } kprintf("0x%x, %d kb", mmap->base_addr_low, mmap->length_low/1024); if (mmap->type==1) { kprintf(" FREE\n"); } else { kprintf(" RESERVED, type %d\n", mmap->type); } mmap = (memory_map_t*) ( (unsigned int)mmap + mmap->size + sizeof(unsigned int) ); } kprintf("Will allocate memory in 0x%x - 0x%x slot\n", largest_mem,largest_mem+largest_mem_size); kprintf("%d pages available\n", largest_mem_size/(4*1024)); #if defined(__i386__) // idt_initialize(); // pic_initialize(); #endif }
void kernel_c(){ //init basic data&struct heap_init(); proc_init(); init_fs(); mem_entity[0]='G'; mem_entity[1]='M'; mem_entity[2]='K'; mem_entity[3]='B'; k_screen_reset(); detect_cpu(); oprintf("detecting cpu...cpu identify:%s\n",cpu_string); // oprintf("mm init..\nring0_pgdir:%x,ring0_pgtbl:%x,base_proc_pgdir:%x,addr_kernel_info:%x,pages:%x\n",RING0_PGDIR,RING0_PGTBL,BASE_PROC_PGDIR,ADDR_KERNEL_INFO,PAGES); global_equal_map(); __asm__("mov $0x101000,%eax\t\n" "mov %eax,%cr3\t\n" "mov %cr0,%eax\t\n" "or $0x80000000,%eax\t\n" "mov %eax,%cr0\t\n" ); oprintf("global page-mapping for kernel built..open MMU\n"); create_kernel_process((int)&idle,9,0xffff,"idle",0); //pid must =0 create_kernel_process((int)hs,2,0xffff,"hs",1);//hs的时间片要非常多,保证在下一轮时间片重置之前不会被挂起 ERR:pid must =1 create_kernel_process((int)fs_ext,4,10,"fs_ext",1);//pid must =2 create_kernel_process((int)mm,3,10,"mm",1);//ERR mm has great prio,because it shall run and prepare condition for other process create_kernel_process((int)tty,5,10,"tty",1); create_kernel_process((int)&p1,8,10,"p1",1); // ofork(t1,9,15,"t1",1); // ofork(t2,9,5,"t2",1); // ofork((int)&p2,7,5,"p2",3); oprintf("basic process ofork done..now open IRQ,proc-dispatch begin\n"); __asm__("sti"); while(1);//内核陷入死循环,等待第一次时钟中断 }
void main (uint32_t mboot_magic, uint32_t mboot_info) { multiboot_memmap_t *first_free; uint64_t *new_stack; asm volatile ("cli"); cpu_early_init(); x64_gdt_init(); x64_idt_init(); vga_console_init(); printk("\n================================\n"); printk("||==Welcome to CELLOS 64 bit==||"); printk("\n================================\n"); /* Read mboot header */ first_free = mboot_init(mboot_info, mboot_magic); if (first_free == NULL) { panic("No free memory for use! STOP~!\n"); } mb_parse_kernel_image(); paging_init(first_free->base_addr, first_free->base_addr + first_free->length); /* Init page allocator */ page_alloc_init(mem_get_low_addr(),mem_get_high_addr()); /* Initialize the memory pool */ init_memory_pool(CONFIG_KHEAP_SIZE, (void *)page_alloc_contig(CONFIG_KHEAP_SIZE/PAGE_SIZE)); detect_cpu(); acpi_init(); smp_init(); paging_late_init(); sched_core_init(); sched_init(); reschedule(); /* No reached */ }
void init (struct multiboot_info *mb_info, uint32_t kernel_esp) { // Clear screen cls(); // HAL-Services kprintln ("Booting up..."); detect_cpu(); InitSerial (115200, 0, 8); InitGlobalDescriptors (kernel_esp); InitInterruptDescriptors(); InitPit (CLOCKS_PER_SEC); InitPmm (mb_info); InitVmm (mb_info); kinit_heap(); #ifdef SELF_TEST if (selfTest() != 0) { kprintln ("Will not start up because of self test errors!"); while (1); } #endif // Erstes Modul ist unsere RAM-Disk InitRamdisk ( ( (struct multiboot_module *) mb_info->mbs_mods_addr)->mod_start); InitMultitasking(); // while(1); // kprintf((char*) ((struct multiboot_module*) // mb_info->mbs_mods_addr)->mod_start); InitBootDrivers(); //int32_test(); __asm ("sti"); for (;;); }
energy_counters_init_res energy_counters_init(energy_counters_state* state){ #if defined(__linux__) u_int64_t result; memset(state, 0, sizeof(energy_counters_state)); state->cpu_model=detect_cpu(); if(state->cpu_model<0){ return UNSUPPORTED_CPU_TYPE; } FILE *f; int n=0; char command[512]; f = popen("cat /proc/cpuinfo | grep 'physical id' | sort -u | wc -l", "r"); if (!f || fscanf(f, "%d", &n) == EOF) { pclose(f); return NUMBER_OF_SOCKETS_NOT_FOUND;} pclose(f); state->num_sockets=n; state->sockets=(socket_state*)malloc(sizeof(socket_state)*state->num_sockets); memset(state->sockets, 0, sizeof(socket_state)*state->num_sockets); unsigned int i; for(i=0; i<state->num_sockets;i++){ sprintf(command, "cat /proc/cpuinfo | egrep 'processor|physical id' | tr -d '\t' " "| tr -d ' ' | paste -d'|' - - | grep 'physicalid:%d' " "| cut -d '|' -f 1 | cut -d ':' -f 2 | head -1", i); f = popen(command,"r"); if (!f || fscanf(f, "%d", &n) == EOF) { pclose(f); return PROCESSOR_PER_SOCKET_NOT_FOUND;} pclose(f); state->sockets[i].core=n; state->sockets[i].fd=open_msr(n); if(state->sockets[i].fd<0){ return IMPOSSIBLE_TO_OPEN_MSR_FILE; } /* Calculate the units used */ result=read_msr(state->sockets[i].fd,MSR_RAPL_POWER_UNIT); state->sockets[i].power_per_unit=pow(0.5,(double)(result&0xf)); state->sockets[i].energy_per_unit=pow(0.5,(double)((result>>8)&0x1f)); state->sockets[i].time_per_unit=pow(0.5,(double)((result>>16)&0xf)); result=read_msr(state->sockets[i].fd,MSR_PKG_POWER_INFO); state->sockets[i].thermal_spec_power=state->sockets[i].power_per_unit*(double)(result&0x7fff); } return OK; #else return OS_NOT_SUPPORTED; #endif }
int main(int argc, char **argv) { int c; int core=-1; int result=-1; int disable=1; int core2=0; printf("\n"); opterr=0; while ((c = getopt (argc, argv, "c:deh")) != -1) { switch (c) { case 'c': core = atoi(optarg); break; case 'd': disable=1; break; case 'e': disable=0; break; case 'h': printf("Usage: %s [-c core] [-d] [-e] [-h]\n\n",argv[0]); exit(0); default: exit(-1); } } core2=detect_cpu(); if (core2<0) { printf("Unsupported CPU type\n"); return -1; } if (disable) { if (core2) { result=disable_prefetch_core2(core); } else { result=disable_prefetch_nhm(core); } } else { if (core2) { result=enable_prefetch_core2(core); } else { result=enable_prefetch_nhm(core); } } if (result<0) { printf("Unable to access prefetch MSR.\n"); printf("* Verify you have an Intel Nehalem or newer processor\n"); printf("* You will probably need to run as root\n"); printf("* Make sure the msr module is installed\n"); printf("\n"); return -1; } return 0; }
void init(multiboot *mboot_ptr, uint32_t init_stack) { init_esp = init_stack; init_gdt(); init_idt(); init_paging(mboot_ptr); init_stdio(); init_timer(FREQ); install_keyboard(); init_video(); ttys = (tty_t**) kmalloc(sizeof(tty_t) * 3); ttys[0] = main_tty; ttys[1] = tty_init(ttys[1]); ttys[2] = tty_init(ttys[2]); tty_index = 0; current_tty = main_tty; printk("%s %s (%s) by %s. Copyright C 2015 %s. All rights reserved.\n", OS_Name, Version, Relase_Date, Author, Author); detect_cpu(); printk("\n-------------------------------------------------------------------\n"); printk("VGA driver was installed!\n"); printk("Initialize tty. "); wstr_color("[OK]\n", COLOR_GREEN); printk("Initialize paging. "); wstr_color("[OK]\n", COLOR_GREEN); printk("Memory info:\n"); printk("\tKernel starts at: %x\n", (size_t)&kernel_start); printk("\tKernel ends at: %x\n", (size_t)&kernel_end); printk("\tRAM: %d MB\n", mem_size_mb); printk("Initialize stdio (allow using of stdio header). "); wstr_color("[OK]\n", COLOR_GREEN); printk("Initialize GDT. "); wstr_color("[OK]\n", COLOR_GREEN); printk("Initialize IDT and interrupts. "); wstr_color("[OK]\n", COLOR_GREEN); printk("Install timer and clock. "); wstr_color("[OK]\n", COLOR_GREEN); printk("Install keyboard support. "); wstr_color("[OK]\n", COLOR_GREEN); /* tasking is useless, because switcher crashes every time init_tasking(); uint32_t cr3, eflags; asm volatile("movl %%cr3, %%eax; movl %%eax, %0;":"=r"(cr3)::"%eax"); asm volatile("pushfl; movl (%%esp), %%eax; movl %%eax, %0; popfl;":"=r"(eflags)::"%eax"); task_t *clock_task = create_task(clock_task, &update_time, eflags, cr3); */ wstr_color("\nDONE!\n", COLOR_GREEN); sti(); getch(); }
int main(int argc, char *argv[]) { char **args[3]; int i=0; int n=0; bool hardware_matches = true; bool multicore = false; bool dryrun = false; bool debug = false; s_cpu cpu; console_ansi_raw(); detect_cpu(&cpu); /* If no argument got passed, let's show the usage */ if (argc == 1) { usage(); return -1; } for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "--")) { argv[i] = NULL; args[n++] = &argv[i + 1]; } else if (!strcmp(argv[i], "64")) { if (debug) printf(" 64bit : %s on this system\n", show_bool(cpu.flags.lm)); hardware_matches = cpu.flags.lm && hardware_matches; } else if (!strcmp(argv[i], "pae")) { if (debug) printf(" pae : %s on this system\n", show_bool(cpu.flags.pae)); hardware_matches = cpu.flags.pae && hardware_matches; } else if (!strcmp(argv[i], "hvm")) { if (debug) printf(" hvm : %s on this system\n", show_bool((cpu.flags.vmx || cpu.flags.svm))); hardware_matches = (cpu.flags.vmx || cpu.flags.svm) && hardware_matches; } else if (!strcmp(argv[i], "multicore")) { if (debug) printf(" multicore : %d cores on this system\n", cpu.num_cores); if (cpu.num_cores > 1) multicore = true; hardware_matches = multicore && hardware_matches; } else if (!strcmp(argv[i], "smp")) { if (debug) printf(" smp : %s on this system\n", show_bool(cpu.flags.smp)); hardware_matches = cpu.flags.smp && hardware_matches; } else if (!strcmp(argv[i], "dry-run")) { dryrun = true; } else if (!strcmp(argv[i], "debug")) { debug = true; } if (n >= 2) break; } while (n < 2) { args[n] = args[n - 1]; n++; } if (debug) { printf("\nBooting labels are : '%s' or '%s'\n", *args[0], *args[1]); printf("Hardware requirements%smatch this system, let's booting '%s'\n", hardware_matches ? " " : " doesn't ", hardware_matches ? *args[0] : *args[1]); printf("Sleeping 5sec before booting\n"); if (!dryrun) sleep(5); } if (!dryrun) boot_args(hardware_matches ? args[0] : args[1]); else printf("Dry-run mode, let's exiting\n"); return -1; }
// Handles a command issued by the user. void cl_handler(char* buffer, fs_node_t *fs_root) { if (strcmp(buffer,"beep") == 0) { beep(); } else if (strcmp(buffer,"clear") == 0) { monitor_clear(); } else if (strcmp(buffer,"contents") == 0) { file_disp(fs_root); } else if (strcmp(buffer,"cpu") == 0) { detect_cpu(); } else if (strcmp(buffer,"fork") == 0) { fork_disp(); } else if (strcmp(buffer,"furelise") == 0) { felise(); } else if (strcmp(buffer,"help") == 0) { help_disp(); } else if (strcmp(buffer,"hibernate") == 0) { // Define variables byte temp; // Disable interrupts cli(); // Clear all keyboard buffers (output and command buffers) do { // Empty user data temp = inb(0x64); // Empty keyboard data if (check_flag(temp, 0) != 0) { inb(0x60); } } while (check_flag(temp, 1) != 0); // Check for Advanced Power Management check_apm(); // Initialize Advanced Power Management init_apm(); // Send hibernation command suspend(); // Halt the cpu if failed halt(); } else if (strcmp(buffer,"logo") == 0) { logo_disp(); } else if (strcmp(buffer,"ls") == 0) { dir_disp(fs_root); } else if (strcmp(buffer,"mary") == 0) { mary(); } else if (strcmp(buffer,"mouse") == 0) { // Initialize the mouse init_mouse(); // output current coordinates //monitor_write_dec(getcoords()); } else if (strcmp(buffer,"nosound") == 0) { nosound(); } else if (strcmp(buffer,"reboot") == 0) { reboot(); } else if (strcmp(buffer,"shutdown") == 0) { // Define variables byte temp; // Disable interrupts cli(); // Clear all keyboard buffers (output and command buffers) do { // Empty user data temp = inb(0x64); // Empty keyboard data if (check_flag(temp, 0) != 0) { inb(0x60); } } while (check_flag(temp, 1) != 0); // Check for Advanced Power Management check_apm(); // Initialize Advanced Power Management init_apm(); // Send shutdown command shutdown(); // Halt the cpu if failed halt(); } else if (strcmp(buffer,"sleep") == 0) { // Define variables byte temp; // Disable interrupts cli(); // Clear all keyboard buffers (output and command buffers) do { // Empty user data temp = inb(0x64); // Empty keyboard data if (check_flag(temp, 0) != 0) { inb(0x60); } } while (check_flag(temp, 1) != 0); // Check for Advanced Power Management check_apm(); // Initialize Advanced Power Management init_apm(); // Send standby command standby(); // Halt the cpu if failed halt(); } else if (strcmp(buffer,"time") == 0) { time_disp(); } else if (strcmp(buffer,"twinkle") == 0) { twinkle(); } else if (strcmp(buffer,"usermode") == 0) { umode_disp(fs_root); } else if (strcmp(buffer,"version") == 0) { version_disp(); } else if (strcmp(buffer,"vga") == 0) { VgaTest(); } else if (strcmp(buffer,"wait") == 0) { timer_wait(250000); } else { printf("Unknown command: "); printf(buffer); printf("\n"); } }