OSDev.org

The Place to Start for Operating System Developers
It is currently Sun Apr 28, 2024 4:39 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: File system image mkfs.c XV6
PostPosted: Tue Jun 20, 2023 11:46 am 
Offline

Joined: Sat May 06, 2023 5:40 pm
Posts: 4
Hello,
There is something that i cant understand.

Why all numerical data in mkfs.c XV6 file system image creator, are converted from big<-->little endian and vice versa.

example :
in the superblock :
Code:
    sb.size = xint(FSSIZE);
    sb.nblocks = xint(nblocks);
    sb.ninodes = xint(NINODES);
    sb.nlog = xint(nlog);
    sb.logstart = xint(2);
    sb.inodestart = xint(2 + nlog);
    sb.bmapstart = xint(2 + nlog + ninodeblocks);


then :
Code:
uint xint(uint x) {
    uint y;
    uchar* a = (uchar*)&y;
    a[0] = x;
    a[1] = x >> 8;
    a[2] = x >> 16;
    a[3] = x >> 24;
    return y;
}


I have try to figure out why but it is pretty closed subject.

I hope this question relevant.


Top
 Profile  
 
 Post subject: Re: File system image mkfs.c XV6
PostPosted: Tue Jun 20, 2023 6:03 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5146
Crixus wrote:
Why all numerical data in mkfs.c XV6 file system image creator, are converted from big<-->little endian and vice versa.

It converts between host-endian and little-endian. On a big-endian host, it converts between big-endian and little-endian. On a little-endian host, the conversion doesn't do anything.

It does that because mkfs needs to be able to run on both big-endian and little-endian CPUs, but the filesystem is always little-endian.


Top
 Profile  
 
 Post subject: Re: File system image mkfs.c XV6
PostPosted: Tue Jun 20, 2023 7:26 pm 
Offline

Joined: Sat May 06, 2023 5:40 pm
Posts: 4
like that ?

Assuming 0x12345678:

On a Big Endian system:
Original x in memory: [12][34][56][78]
After xint(), y in memory: [78][56][34][12] (bytes are reversed)
This will appear as a change when viewing memory.

On a Little Endian system:
Original x in memory: [78][56][34][12]
After xint(), y in memory: [78][56][34][12] (bytes stay in same order)
This will appear as no change when viewing memory.


And so the disk image is in little endian... when a big endian proc have to use it, it should reverse all numerical data every times ?


Top
 Profile  
 
 Post subject: Re: File system image mkfs.c XV6
PostPosted: Tue Jun 20, 2023 10:20 pm 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5146
Crixus wrote:
like that ?

Yep, you've got it.

Crixus wrote:
And so the disk image is in little endian... when a big endian proc have to use it, it should reverse all numerical data every times ?

Yes, but xv6 only runs on little-endian CPUs.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 20 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group