howto map memory properly

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
kiwipresse
Member
Member
Posts: 25
Joined: Sun Nov 04, 2007 7:41 am

howto map memory properly

Post by kiwipresse »

hi,

I'm facing some problems when trying to map memory from 0x100000 to something different (not 1:1).

Here is some code:

Code: Select all

/* PDN is just a macro which handles the shifting stuff */

phyaddr = 0;
for(i = PDN(0xC0000000); i<1024; i++)
{
    for(j=0; j<1024; j++)
    {
        pt[j] = (paddress << 12) | 3;
        paddress += 0x1000;
    }

    pd[i] = pt[-PDN(0xC0000000)+i] | 3;
}

this code should map the first 4MB to 0xC0000000 but qemu hangs when running this code. I'm sure that I didn't quite understand howto map memory correctly :(
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Post by Combuster »

Code: Select all

pt[j] = (paddress << 12) | 3;
paddress += 0x1000; 
why are you shifting?
pt[0] = 0 | 3
pt[1] = 0x1000 << 12 | 3 = 0x1000003
pt[2] = 0x2000003
first page points to 0k..4k
second page points to 16M..16.004M instead of 4k..8k
third page points to 32M..32M+4k instead of 8k..12k
so you're not mapping the first MB, you're mapping random stuff. :!:
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
kiwipresse
Member
Member
Posts: 25
Joined: Sun Nov 04, 2007 7:41 am

Post by kiwipresse »

Code: Select all

pt[j] = paddress | 3;
Okay, new code same result - qemu hangs. If I do just 1:1 paging everything is fine.
User avatar
AndrewAPrice
Member
Member
Posts: 2327
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

Post by AndrewAPrice »

Are you getting a page-fault? Because when you copy it to the new location, your kernel still might be executing code from the old memory location.
My OS is Perception.
User avatar
fontknocker
Posts: 16
Joined: Mon Dec 11, 2006 1:49 am
Location: Canberra, Australia

Post by fontknocker »

Truthfully, I'm struggling to understand exactly what that code does. Maybe I'm just tired :). If I were you I would build my page table first and insert into the page directory later:

Code: Select all

for(int frame = 0; frame < 1024; frame++)
    // build pt here.

// then insert pt into the page directory later on:
pd[768] = ptPhysAddress | 0x3    // lowest 4MB to 0xC0000000
You shouldn't need a nested loop like that to map the first 4MB of address space.
kiwipresse
Member
Member
Posts: 25
Joined: Sun Nov 04, 2007 7:41 am

Post by kiwipresse »

@MessiahAndrw: no page fault, before this code is run the memory is mapped 1:1 and qemu showed me that this memory area is mapped correctly

@fontknocker: Okay I've rewritten my code a little bit...

Code: Select all

  paddress = 0;
  for(i=0; i<1024; i++)
    {
      pt[PTN(paddress)+(i*0x1000)] = paddress | 3;
      paddress += 0x1000;
    }

  pd[768] = (unsigned long)pt | 3;
Now it seems to function, info mem showed me this:

Code: Select all

00000000-00400000 00400000 -rw
c0000000-c0400000 00400000 -rw
Now I can call the code in the 0xC0000000 region and it seems that everything is fine, did I miss something?

BTW has somebody more in-depth information about memory mapping?

Thank you for your help!
kiwipresse
Member
Member
Posts: 25
Joined: Sun Nov 04, 2007 7:41 am

Post by kiwipresse »

Due to my last success I started to integrate my older code to the newer one (higher half kernel).

I thought that this is basically easy since 1:1 paging is still active thus I don't have to add any offset to fixed memory addresses. Most of my code run without any problems but one function makes problems (putc). Whenever I add it GRUB reports: Invalid or unsupported executable format.

Here is a complete lists of all symbols within the kernel file:

Code: Select all

001041d2 A __early_init_end
00100000 A __early_init_start
c0108000 A __end
c01041d2 A __start
c0105030 T _interrupt0
c0105039 T _interrupt1
c0105078 T _interrupt10
c010507e T _interrupt11
c0105084 T _interrupt12
c010508a T _interrupt13
c0105090 T _interrupt14
c0105096 T _interrupt15
c0105042 T _interrupt2
c010504b T _interrupt3
c0105054 T _interrupt4
c010505a T _interrupt5
c0105060 T _interrupt6
c0105066 T _interrupt7
c010506c T _interrupt8
c0105072 T _interrupt9
00100000 T _start
c0104f28 T _trap0
c0104f31 T _trap1
c0104f80 T _trap10
c0104f87 T _trap11
c0104f8e T _trap12
c0104f95 T _trap13
c0104f9c T _trap14
c0104fa3 T _trap15
c0104fac T _trap16
c0104fb5 T _trap17
c0104fbe T _trap18
c0104fc7 T _trap19
c0104f3a T _trap2
c0104fd0 T _trap20
c0104fd9 T _trap21
c0104fe2 T _trap22
c0104feb T _trap23
c0104ff4 T _trap24
c0104ffd T _trap25
c0105006 T _trap26
c010500f T _trap27
c0105018 T _trap28
c010501e T _trap29
c0104f43 T _trap3
c0105024 T _trap30
c010502a T _trap31
c0104f4c T _trap4
c0104f55 T _trap5
c0104f5e T _trap6
c0104f67 T _trap7
c0104f70 T _trap8
c0104f77 T _trap9
c0104e94 T add_interrupt_handler
c0105114 T add_trap_handler
00104030 T early_init
c0106004 D ega_mm
c010509c t fault_handle
c0104de0 T inb
c01041d4 T init
c0104e1c T inl
c01050d7 t interrupt_handle
c0104eb4 T interrupt_handler
c0107000 B interrupt_handlers
c0104dfd T inw
c0104304 t isdigit
0010001f t loop
c0104297 T memcpy
c01042d7 T memset
00100010 t multiboot_entry
00100004 t multiboot_header
c01043c2 t number
c0104e38 T outb
c0104e7c T outl
c0104e59 T outw
c0104d48 T printk
c0105138 T putc
c0104367 t skip_atoi
00100030 T stack
00100000 T start
c0104268 T strlen
c010432c t strnlen
c0105131 T trap_handler
c0107400 B trap_handlers
c010472b T vsnprintf
c0107480 b xpos
c0106000 d ypos
What confuses me is that this message is only printed when putc is compiled.

The source of putc:

Code: Select all

#define COLUMNS       80
#define LINES         24

#define ATTRIBUTE     7

#define MONO_MEMORY   0xB0000
#define COLOUR_MEMORY 0xB8000

#if CONFIG_ARCH_CONS_MONO == 1
#define VIDEO_MEMORY MONO_MEMORY
#else
#define VIDEO_MEMORY COLOUR_MEMORY
#endif

static int xpos = 0;
static int ypos = 8;

unsigned char *ega_mm= (unsigned char *)VIDEO_MEMORY;

void putc(int c)
{
  if (c == '\n' || c == '\r')
    {
    newline:
      xpos = 0;
      ypos++;
      if (ypos >= LINES)
        ypos = 0;
      return;
    }
  else if (c == '\t') {
    xpos += (8 - (xpos & 7));
    return;
  }

  *(ega_mm + (xpos + ypos * COLUMNS) * 2) = c & 0xFF;
  *(ega_mm + (xpos + ypos * COLUMNS) * 2 + 1) = ATTRIBUTE;

  xpos++;
  if (xpos >= COLUMNS)
    goto newline;
}
This is the only place where putc is called:

Code: Select all

int printk(const char *format, ...)
{
  va_list ap;
  int n, i;
  char printk_buf[1024];

  va_start(ap, format);
  n = vsnprintf(printk_buf, sizeof printk_buf, format, ap);
  va_end(ap);

  for(i=0; i<strlen(printk_buf); i++)
      putc(printk_buf[i]);

  return n;
}
Hope anyone can help.
User avatar
JAAman
Member
Member
Posts: 880
Joined: Wed Oct 27, 2004 11:00 pm
Location: WA

Post by JAAman »

Whenever I add it GRUB reports: Invalid or unsupported executable format.
first suspect is:

is that code being placed before the multi-boot header? thereby pushing it beyond the limit? (i dont use grub, but iirc it must be in the first 4k of the file?)
Craze Frog
Member
Member
Posts: 368
Joined: Sun Sep 23, 2007 4:52 am

Post by Craze Frog »

Your multiboot header is too far from the start. Place it completely at the start by using a separate section for it, and a linker script similar to this:

Code: Select all

OUTPUT_FORMAT("elf32-i386")
ENTRY(start)
phys = 0x00100000;
SECTIONS
{
  .text phys : AT(phys) {
    code = .;
    *(.multiboot)
    *(.text)
    *(.code)
    *(.rodata*)
    /* . = ALIGN(4096); */
  }
  .data : AT(phys + (data - code))
  {
    data = .;
    *(.data)
    /* . = ALIGN(4096); */
  }
  .bss : AT(phys + (bss - code))
  {
    bss = .;
    . = ALIGN(4096);
    *(.bss)
  }
  end = .;
}

kiwipresse
Member
Member
Posts: 25
Joined: Sun Nov 04, 2007 7:41 am

Post by kiwipresse »

My multiboot header is too far from the start? My multiboot header is almost at the beginning...

Code: Select all

00100004 t multiboot_header
The corresponding part of my linker script:

Code: Select all

  . = 0x100000;

  __early_init_start = .;
  .early_init :
  {
    grub.o
    early_init.o
  }
  __early_init_end = .;
multiboot_header is defined within grub.S

Did I miss something?
User avatar
fontknocker
Posts: 16
Joined: Mon Dec 11, 2006 1:49 am
Location: Canberra, Australia

Post by fontknocker »

The Multiboot header needs to be within the first 8192 bytes of the binary image and it must be 32-bit aligned. Run mbchk on your kernel image and it will tell you if your Multiboot headers are correct.

Like others have said, you can correct this by placing the Multiboot header into its own section and linking that section before any others.
kiwipresse
Member
Member
Posts: 25
Joined: Sun Nov 04, 2007 7:41 am

Post by kiwipresse »

I still don't understand clearly why it doesn't run with my current linker script.
early_init should be the very first section which contains everything of grub.o and early_init.o. Both files are certainly less than 8192 bytes:

Code: Select all

1128 early_init.o
792 grub.o
and why does it function except when putc.o is compiled in? Indeed I also tried to declare a multiboot section without any success.
User avatar
fontknocker
Posts: 16
Joined: Mon Dec 11, 2006 1:49 am
Location: Canberra, Australia

Post by fontknocker »

kiwipresse wrote:I still don't understand clearly why it doesn't run with my current linker script.
Off the top of my head, I don't either. Maybe someone else does. I'm thinking it could be a 32-bit alignment problem. Are you sure you 4-byte aligned your multiboot header in your assembly code?

Code: Select all

SECTION .multiboot
ALIGN 4
    dd MBOOT_MAGIC
    ; more stuff....
Additionally, what does mbchk tell you?
kiwipresse
Member
Member
Posts: 25
Joined: Sun Nov 04, 2007 7:41 am

Post by kiwipresse »

I found the mistake, it was a simple calculation error in my linking script. It has overwritten the multiboot header :?

now mbchk finds the multiboot_header in both the grub.o file as well as in the final kernel image. Thanks!
Post Reply