memcpy-ing intel x86 opcodes into memory causes general protection fault

Viewed 62

I'm trying to make a simple program loader for my OS and I have code which copies a 128-member u16 array into an address (currently 0x4400) by iterating through the array copying a number into the address and adding sizeof(u8) like this

void loader(u16 raw[128]) {
    // memory_copy(clear, 0x4100, sizeof(clear)); // just to like not mess up stuff
    // memory_copy(raw, 0x4100, sizeof(raw));
    for (int idx = 0; idx > sizeof(raw); idx += 1) {
        u8 *m_idx = 0x4400;
        memory_copy(raw[idx], m_idx, sizeof(raw));
        m_idx = m_idx += sizeof(u8);
    }
    // asm volatile ("pusha");
    asm volatile ("jmp 0x4400"); 
    // asm volatile ("popa");
}

Edit: This is my memory_copy function

void memory_copy(u8 *source, u8 *dest, int nbytes) {
    int i;
    for (i = 0; i < nbytes; i++) {
        *(dest + i) = *(source + i);
    }
}

Edit Again: I fixed the code (changed the address because i think the OSes code was in it)

void loader(u16 raw[128]) {
    memory_copy(raw, (u8*)0x5000, (sizeof(u16) * 128));
    asm volatile ("jmp 0x5000"); 
}
0 Answers
Related