Page 1 of 1

Setting Up Task State Segment - Crash Without Fault

Posted: Mon Jan 23, 2017 6:03 am
by SukantPal
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: Select all

#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: Select all

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

Re: Setting Up Task State Segment - Crash Without Fault

Posted: Mon Jan 23, 2017 6:04 am
by SukantPal

Code: Select all

/* '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

Re: Setting Up Task State Segment - Crash Without Fault

Posted: Mon Jan 23, 2017 6:09 am
by SukantPal
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)

Re: Setting Up Task State Segment - Crash Without Fault

Posted: Mon Jan 23, 2017 9:14 am
by JAAman
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

Re: Setting Up Task State Segment - Crash Without Fault

Posted: Mon Jan 23, 2017 9:27 am
by JAAman
"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

Re: Setting Up Task State Segment - Crash Without Fault

Posted: Mon Jan 23, 2017 10:02 am
by iansjack
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.

Re: Setting Up Task State Segment - Crash Without Fault

Posted: Fri Jan 27, 2017 4:29 am
by SukantPal
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.