OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 6:25 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: Setting Up Task State Segment - Crash Without Fault
PostPosted: Mon Jan 23, 2017 6:03 am 
Offline
Member
Member

Joined: Sat Jan 21, 2017 7:35 am
Posts: 35
I am setting up the TSS. If I setup GDT without TSS everything works fine. Just as I remove the comment from FlushTSS() the kernel crashes. I have setup a Segment Fault Handler, Stack Handler, Invalid TSS Handler, and Double Fault Handler, but none show up.

The code -
Code:
#ifdef x86

#include <Segmentation.hpp>
#include <Interrupt.hpp>
#include <Registration.hpp>

extern "C" void FlushTSS();

/* Don't compile Native.asm on non-x86 */
extern "C" {
   struct NativeDescTablePointer GDTPtr;
   struct SegmentDescriptor GDTable[6];
   struct TSS systemTSS;
}

extern "C" void setGate(uint32_t gateNo, uint32_t base, uint32_t limit, uint8_t access, uint8_t granularity) {
   GDTable[gateNo].lowerBase = base & 0xffff;
   GDTable[gateNo].middleBase = (base >> 16) & 0xff;
   GDTable[gateNo].higherBase = (base >> 24) & 0xff;

   GDTable[gateNo].lowerLimit = limit & 0xffff;
   GDTable[gateNo].granularity = (limit >> 16) & 0xf;

   GDTable[gateNo].granularity |= granularity & 0xf0;
   GDTable[gateNo].access = access;
}

extern "C" void setupTSS(uint32_t offset, uint16_t ss0, uint16_t esp0) {
   uint32_t base = (uint32_t) &systemTSS;
   uint32_t size = base + sizeof(TSS);
      
   setGate(offset, base, size, 0xe9, 1);

   unsigned char *tssLocation = (unsigned char*) &systemTSS;
   for(int offset = 0; offset < sizeof(systemTSS); offset++) {
      tssLocation[offset] = 0;
   }

//   systemTSS.eflags = 0x1202;

   systemTSS.ss0 = ss0;
   systemTSS.esp0 = esp0;

   systemTSS.cs = 0x0B;
   systemTSS.ss = // These specify what segments
   systemTSS.ds = // should be loaded when the
   systemTSS.es = // processor switches to
   systemTSS.fs = // kernel mode.
   systemTSS.gs =
   0x13;

   systemTSS.iomap = sizeof(TSS);
}

extern "C" void setupSegmentation() {
   #ifdef segmentationEnabled
      GDTPtr.limit = (sizeof(struct SegmentDescriptor) * 3) - 1;
      GDTPtr.base = (uint32_t) GDTable;

      setGate(0, 0, 0, 0, 0); // NULL Descriptor
      setGate(1, 0, 0xffffffff, 0x9a, 0xcf);
      setGate(2, 0, 0xffffffff, 0x92, 0xcf);
      setGate(3, 0, 0xffffffff, 0xfa, 0xcf);
      setGate(4, 0, 0xffffffff, 0xf2, 0xcf);
      setupTSS(5, 0x10, 0x0); // fine without FlushTSS

      FlushGDT();
      FlushTSS(); // This is a problem - { mov ax, 0x2b; ltr ax; ret }
   #endif
}

Can someone tell me what mistake is there in the code? Also I can show a part of Native.asm -
Code:
extern GDTPtr

   FlushGDT:
      lgdt [GDTPtr]
      mov ax, 0x10
      mov ds, ax
      mov es, ax
      mov fs, ax
      mov gs, ax
      mov ss, ax
      jmp 0x08:SegmentReturn
   SegmentReturn:
      ret

   FlushTSS:
      mov ax, 0x2B
      ltr ax
      ret


edit JAAman: please use code tags next time


Top
 Profile  
 
 Post subject: Re: Setting Up Task State Segment - Crash Without Fault
PostPosted: Mon Jan 23, 2017 6:04 am 
Offline
Member
Member

Joined: Sat Jan 21, 2017 7:35 am
Posts: 35
Code:
/* 'Global Descriptor Table' - Task State Segment */
   struct TSS
   {
      uint32_t prev_tss;
      uint32_t esp0;       
      uint32_t ss0;
      uint32_t esp1;
      uint32_t ss1;
      uint32_t esp2;
      uint32_t ss2;
      uint32_t cr3;
      uint32_t eip;
      uint32_t eflags;
      uint32_t eax;
      uint32_t ecx;
      uint32_t edx;
      uint32_t ebx;
      uint32_t esp;
      uint32_t ebp;
      uint32_t esi;
      uint32_t edi;
      uint32_t es;         
      uint32_t cs;       
      uint32_t ss;       
      uint32_t ds;       
      uint32_t fs;       
      uint32_t gs;         
      uint32_t ldt;     
      uint16_t trap;
      uint16_t iomap;
   } __attribute__((__packed__));
   typedef TSS TaskSegment;

   /* 'Global Descriptor Table' - Segment Descriptor */
   struct SegmentDescriptor {
      unsigned short lowerLimit;
      unsigned short lowerBase;
      unsigned char middleBase;
      unsigned char access;
      unsigned char granularity;
      unsigned char higherBase;
   } __attribute__((packed));

This is header


Top
 Profile  
 
 Post subject: Re: Setting Up Task State Segment - Crash Without Fault
PostPosted: Mon Jan 23, 2017 6:09 am 
Offline
Member
Member

Joined: Sat Jan 21, 2017 7:35 am
Posts: 35
I would also like to state here that this kernel uses 'barebones higher half' and paging is enabled with pse if that makes a difference. (No Page Fault)


Top
 Profile  
 
 Post subject: Re: Setting Up Task State Segment - Crash Without Fault
PostPosted: Mon Jan 23, 2017 9:14 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 27, 2004 11:00 pm
Posts: 874
Location: WA
from now on, please use code tags when you post code

I have edited your posts to add the code tags this time, but in the future you are expected to use them yourself

_________________
## ---- ----- ------ Intel Manuals
OSdev wiki


Top
 Profile  
 
 Post subject: Re: Setting Up Task State Segment - Crash Without Fault
PostPosted: Mon Jan 23, 2017 9:27 am 
Offline
Member
Member
User avatar

Joined: Wed Oct 27, 2004 11:00 pm
Posts: 874
Location: WA
"crash" is not a description, if you need help, you need to be descriptive about:
1) what is happening
2) what you expect to happen
3) what you think might be happening
4) what you have already tried

I am guessing that you are getting a triple-fault, but that is only a guess, since you didn't bother telling us what happened (answering the 4 questions above)

if I am right about that, then in this case, rather than posting code, it would be more helpful to post a partial Bochs log (the part including the error you received -- and please use code tags for that as well) -- people aren't going to want to look at all your code and figure out what you are doing and how when they don't even know what you mean by "crash"... however, the experienced people on this forum will likely be able to tell you exactly what you did wrong by looking at the Bochs log without seeing any of your code at all (or at the very least, will be able to reduce the likely number of places to look for a problem)


in general, you will find people are much more likely to help you (and will be much more helpful) if you don't post code -- if seeing the code might help, they might ask you to post a portion of it, or you might give a link to a code repo where they can find it, but simply posting code without being asked and expecting help is likely to backfire and make other people not want to help you at all

_________________
## ---- ----- ------ Intel Manuals
OSdev wiki


Top
 Profile  
 
 Post subject: Re: Setting Up Task State Segment - Crash Without Fault
PostPosted: Mon Jan 23, 2017 10:02 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
I'm disappointed to see that you have ignored all of the advice I gave in your other thread. It's your choice, of course, to ignore advice, just as it is my choice to ignore any further posts from you.


Top
 Profile  
 
 Post subject: Re: Setting Up Task State Segment - Crash Without Fault
PostPosted: Fri Jan 27, 2017 4:29 am 
Offline
Member
Member

Joined: Sat Jan 21, 2017 7:35 am
Posts: 35
I didn't ignore your advice. I'm sorry that I made a big mistake. I'd agree that the posts were not upto the point. But your advice came after this post, so I didn't read that before posting.

Also, I really didn't know what code tags were. I thought that they came on their own.


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 160 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