Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // bareBones DOS multi core setup original by bloodwych around 2013 Foldername
- // Beware that the worker core operates in a bare metal Ring 0 flat mode
- // License: GPL3+
- // build ok 2026 using djgpp gcc 2core_demo.c apboot.s -o 2coreD.exe -s
- #include <stdint.h>
- #include <stdio.h>
- #include <string.h>
- #include <dpmi.h>
- #include <dos.h>
- #include <sys/farptr.h>
- #include <go32.h>
- #include <pc.h>
- #include <conio.h>
- extern uint8_t apStart[];
- extern uint8_t apEnd[];
- extern uint8_t ap32[];
- extern uint8_t gdt[];
- extern uint8_t patchJmp[];
- extern uint8_t patchGdt[];
- extern uint8_t patchStack[];
- extern uint8_t patchMain[];
- /*
- Entry Point fuer den Application Processor
- */
- void apMain() {
- uint16_t *vga;
- uint32_t delay;
- uint32_t tick;
- uint32_t ecgPos;
- uint16_t traceChar;
- uint32_t baseOfs;
- uint32_t upSecs;
- uint32_t upMins;
- uint32_t tscLow;
- uint32_t tscHigh;
- uint64_t tscNow;
- uint64_t tscOld;
- uint32_t cpuHz;
- volatile uint8_t *state;
- volatile uint8_t *alive;
- // paging is noch off also direct memory map auf vga buffer
- vga = (uint16_t *)0xB8000;
- tick = 0;
- ecgPos = 2;
- baseOfs = 40;
- upSecs = 0;
- upMins = 0;
- cpuHz = 2000000000;
- state = (volatile uint8_t *)0x90100;
- alive = (volatile uint8_t *)0x90101;
- cpuHz = *((volatile uint32_t *)0x90104); // bogomips
- *alive = 1;
- for(delay = 2; delay < 22; delay++) {
- vga[baseOfs + delay] = 0x0A00 | '_';
- }
- // offset fur den delta verglech im loop festlegen
- __asm__ __volatile__("rdtsc" : "=a"(tscLow), "=d"(tscHigh));
- tscOld = ((uint64_t)tscHigh << 32) | tscLow;
- while(*state != 0xFF) {
- // rdtsc lesen blockt background interrupt noize aus
- __asm__ __volatile__("rdtsc" : "=a"(tscLow), "=d"(tscHigh));
- tscNow = ((uint64_t)tscHigh << 32) | tscLow;
- // target frame pacing auf 25 hz
- if (tscNow - tscOld >= (cpuHz / 25)) {
- tscOld = tscNow;
- // ipc signal ueberpruefen
- if (*state == 1) {
- // trigger herz phasen zyklisch
- if(tick == 0) {
- vga[baseOfs + 1] = 0x0E03;
- // herz zeichnen
- traceChar = 0x0A00 | 'X';
- } else if(tick == 2) {
- vga[baseOfs + 1] = 0x0604;
- traceChar = 0x0A00 | '_';
- } else if(tick == 6) {
- vga[baseOfs + 1] = 0x0720;
- // draw clear phase erzeugt pump efekt
- traceChar = 0x0A00 | '_';
- } else {
- traceChar = 0x0A00 | '_';
- }
- // sweep visualisieren
- vga[baseOfs + ecgPos] = traceChar;
- // abstand zum crt beam erzeugen
- if(ecgPos < 21) {
- vga[baseOfs + ecgPos + 1] = 0x0720;
- } else {
- vga[baseOfs + 2] = 0x0720;
- }
- ecgPos++;
- if(ecgPos > 21) {
- ecgPos = 2;
- }
- } else {
- // loesche grafik sweep
- for(delay = 1; delay < 22; delay++) {
- vga[baseOfs + delay] = 0x0720;
- }
- // springe an position 72 direkt for die uhr
- if(tick == 0) {
- vga[72] = 0x0E03;
- } else if(tick == 2) {
- vga[72] = 0x0604;
- } else if(tick == 6) {
- vga[72] = 0x0720;
- }
- }
- tick++;
- if(tick > 24) {
- tick = 0;
- upSecs++;
- if(upSecs > 59) {
- upSecs = 0;
- upMins++;
- }
- vga[74] = 0x0A00 | (48 + (upMins / 10));
- vga[75] = 0x0A00 | (48 + (upMins % 10));
- vga[76] = 0x0A00 | ' ';
- vga[77] = 0x0A00 | (48 + (upSecs / 10));
- vga[78] = 0x0A00 | (48 + (upSecs % 10));
- vga[79] = 0x0a00;
- }
- }
- // smt thread pause hint
- __asm__ __volatile__("pause");
- }
- // CPU anhalten
- __asm__ __volatile__(
- "cli\n\t"
- ".Lap_halt: hlt\n\t"
- "jmp .Lap_halt"
- );
- // Endemarker wird genutzt um die groesse der funktion zu berechnen
- __asm__ __volatile__("jmp 1f \n\t .long 0xABBABEAF \n\t 1:");
- }
- /*
- Baut das Trampolin im Low Memory auf
- */
- void prepTram(uint32_t alignPhys) {
- uint32_t sizePayld;
- uint32_t ofsJmp, ofsGdt, ofs32, ofsTabl, ofsStack, ofsMain, ofsCFunc;
- uint32_t stackTop;
- uint8_t *scanPointer;
- uint32_t funcSize;
- sizePayld = (uint32_t)(apEnd - apStart);
- ofsJmp = (uint32_t)(patchJmp - apStart);
- ofsGdt = (uint32_t)(patchGdt - apStart);
- ofs32 = (uint32_t)(ap32 - apStart);
- ofsTabl = (uint32_t)(gdt - apStart);
- ofsStack = (uint32_t)(patchStack - apStart);
- ofsMain = (uint32_t)(patchMain - apStart);
- ofsCFunc = 512;
- stackTop = alignPhys + 0xf000;
- scanPointer = (uint8_t *)&apMain;
- funcSize = 0;
- // durchsuche memory byteweise weill das memory alignment varieren kan
- while(*(uint32_t*)scanPointer != 0xABBABEAF) {
- funcSize++;
- scanPointer++;
- }
- // marker selbst inken
- funcSize += 4;
- // printf("DEBUG prepTram payload scan done at %d bytes \n", funcSize);
- // payload muss exact an die page boundary da die CPU im real mode startet
- _dosmemputb(apStart, sizePayld, alignPhys);
- _dosmemputb((void*)&apMain, funcSize, alignPhys + ofsCFunc);
- // Assembly code braucht absolute addressen weil relocations im real mode nicht greifen
- _farpokel(_dos_ds, alignPhys + ofsJmp, alignPhys + ofs32);
- _farpokel(_dos_ds, alignPhys + ofsGdt, alignPhys + ofsTabl);
- _farpokel(_dos_ds, alignPhys + ofsStack, stackTop);
- _farpokel(_dos_ds, alignPhys + ofsMain, alignPhys + ofsCFunc);
- }
- /*
- Mappe physikalischen Speicher in DPMI
- */
- int mapMem(uint32_t physAddr) {
- __dpmi_meminfo memInfo;
- int apicSel;
- memInfo.address = physAddr;
- memInfo.size = 4096;
- // APIC mmio bereich
- if (__dpmi_physical_address_mapping(&memInfo) != 0) {
- return -1;
- }
- apicSel = __dpmi_allocate_ldt_descriptors(1) ;
- __dpmi_set_segment_base_address(apicSel, memInfo.address);
- __dpmi_set_segment_limit(apicSel, 4095);
- return apicSel;
- }
- /*
- Wartet Mikrosekunden ueber IO Port
- */
- void microWait(uint32_t us) {
- uint32_t i;
- for (i = 0; i < us; i++) {
- // dummy port read erzwingt delay auf dem ISA bus
- inportb(0x80);
- }
- }
- /*
- Wartet Millisekunden
- */
- void milliWait(uint32_t ms) {
- uint32_t i;
- for (i = 0; i < ms; i++) {
- microWait(1000);
- }
- }
- /*
- Sendet die IPI Hardware Sequenz an den Core
- */
- void startAp(int apicSel, uint8_t trampPage) {
- uint32_t icrLow;
- printf("DEBUG startAp initiiere hardware sequenz \n");
- fflush(stdout);
- // APIC fehler state leeren
- _farpokel(apicSel, 0x0F0, 0x1FF);
- // INIT
- icrLow = 0x000C4500;
- _farpokel(apicSel, 0x300, icrLow);
- milliWait(10);
- // Der vector teil im SIPI zwingt den instruction pointer vom worker core auf unsere trampoline page
- icrLow = 0x000C4600 | trampPage;
- _farpokel(apicSel, 0x300, icrLow);
- milliWait(1);
- // spec sagt redundant senden da der APIC bus beim ersten packet noch busy sein koennte
- _farpokel(apicSel, 0x300, icrLow);
- milliWait(10);
- }
- /*
- CPU Takt grob ueber PIT + TSC messen
- */
- uint32_t estimateCpuHz() {
- uint32_t tscStart;
- uint32_t tscEnd;
- uint32_t cpuHz;
- outportb(0x43, 0x30);
- outportb(0x40, 0xFF);
- outportb(0x40, 0xFF);
- __asm__ __volatile__(
- "rdtsc"
- : "=a"(tscStart)
- :
- : "edx"
- );
- while(((uint16_t)inportb(0x40) |
- ((uint16_t)inportb(0x40) << 8)) > 0x1000) {
- __asm__ __volatile__("pause");
- }
- __asm__ __volatile__(
- "rdtsc"
- : "=a"(tscEnd)
- :
- : "edx"
- );
- cpuHz = (tscEnd - tscStart) * 18;
- if(cpuHz < 1000000) {
- cpuHz = 100000000;
- }
- return cpuHz;
- }
- int main() {
- uint32_t physAddr;
- int apicSel;
- uint32_t alignPhys;
- uint8_t trampPage;
- uint32_t tick;
- uint32_t ecgPos;
- char traceChar;
- int waitMs;
- uint32_t cpuHz;
- physAddr = 0xFEE00000;
- printf("DEBUG main starte \n");
- fflush(stdout);
- alignPhys = 0x90000;
- trampPage = alignPhys / 4096;
- apicSel = mapMem(physAddr);
- prepTram(alignPhys);
- // CPU speed schaetzeisen rechne mit overhead
- cpuHz = estimateCpuHz()*1.3;
- printf("DEBUG CPU Speed geschaetzt: %lu Hz\n", cpuHz);
- fflush(stdout);
- // shared memory setup
- _farpokeb(_dos_ds, alignPhys + 0x100, 1);
- _farpokeb(_dos_ds, alignPhys + 0x101, 0);
- // cpu speed fuer AP core
- _farpokel(_dos_ds, alignPhys + 0x104, cpuHz);
- // printf("DEBUG tramp page is %d \n", trampPage);
- startAp(apicSel, trampPage);
- waitMs = 0;
- while(_farpeekb(_dos_ds, alignPhys + 0x101) == 0 &&
- waitMs < 500) {
- delay(10);
- waitMs += 10;
- }
- if(_farpeekb(_dos_ds, alignPhys + 0x101) == 0) {
- printf("FEHLER Core 2 meldet sich nicht \n");
- return -1;
- }
- printf("DEBUG main AP Boot erfolgreich durchlaufen \n");
- printf("Press any key to exit cleanly Core 2 will stay alive \n");
- fflush(stdout);
- tick = 0;
- ecgPos = 2;
- textcolor(10);
- for(int i = 2; i < 22; i++) {
- gotoxy(i + 1, 1);
- putch('_');
- }
- while(!kbhit()) {
- if(tick == 0 || tick == 1) {
- gotoxy(2, 1);
- textcolor(14);
- putch(3);
- if(tick == 0) {
- traceChar = 'X';
- } else {
- traceChar = '_';
- }
- } else if(tick == 2 || tick == 3) {
- gotoxy(2, 1);
- textcolor(6);
- putch(4);
- traceChar = '_';
- } else if(tick == 6) {
- gotoxy(2, 1);
- textcolor(7);
- putch(' ');
- traceChar = '_';
- } else {
- traceChar = '_';
- }
- if(ecgPos < 21) {
- gotoxy(ecgPos + 2, 1);
- putch(' ');
- } else {
- gotoxy(3, 1);
- putch(' ');
- }
- gotoxy(ecgPos + 1, 1);
- textcolor(10);
- putch(traceChar);
- ecgPos++;
- if(ecgPos > 21) {
- ecgPos = 2;
- }
- tick++;
- if(tick > 25) {
- tick = 0;
- }
- delay(40);
- }
- // Uptime Mode aktivieren
- _farpokeb(_dos_ds, alignPhys + 0x100, 0x0);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment