Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7523483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:48:56+00:00 2026-05-30T02:48:56+00:00

I’m trying to handle Kernel interrupts via the IDT. I work on Intel x86

  • 0

I’m trying to handle Kernel interrupts via the IDT.
I work on Intel x86 under Linux.

I have set my IDT and my interrupts entries and I launched some tests to view my interrupts handlers.

When I try int $0x0, it works perfectly: my handler is called but when I try some exception with error code pushed, I enter in an infinite loop.

The schema is the following:

When an exception arrives, the first part of my handler is in ASM and calls a common C part.

my_handler.c

void handler(int i)
{
  printf("Exception %d caught\n", i);
}

my_handlers.S

common:
    pushal

    pushl %ds
    pushl %es
    pushl %fs
    pushl %gs

    addl $48, %esp                  // 4 4-bytes segments pushed
                                    // + 8 4-bytes registers (pushal)
`                                   // esp points on exception code

    call handler                    // call the C handler with exception code

    subl $48, %esp

    popl %gs
    popl %fs
    popl %es
    popl %ds

    popal

    addl $8, %esp                   // 4-byte error code + 4-byte exception number
    iret


exception_de_handler:
    pushl $0                        // Fake error code
    pushl $0                        // interrupt number
    jmp common

exception_gp_handler:
    // error code is pushed by µproc.
    pushl $13                       // interrupt number
    jmp common

exception_pf_handler:
    // error code is pushed by µproc.
    pushl $14                       // interrupt number
    jmp common

If I try to run the followig code:

int* a = 0x0;
*a = 42;

It works, the exceution resumes after the *a = 42;

But if I try:

int* a = 0x0;
*a = 42;
*a = 1337;

It goes in an infinite loop:

Exception 14 caught
Exception 13 caught
Exception 13 caught
Exception 13 caught
Exception 13 caught
        .....
Exception 13 caught
Exception 13 caught
Exception 13 caught
        .....

Why is first exception Page Fault(14) handled then looping on General Protection(13)?

Thank you for your answers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T02:48:57+00:00Added an answer on May 30, 2026 at 2:48 am

    I think you are messing up your stack. You need to be very careful about what you do with your stack in an interrupt handler. In this case it seems you do the following:-

    Push error code (can be done by CPU)
    Push regs
    Push segment regs

    add 0x48 to the stack pointer, to wind the stack all the way back up so it points to the error code.

    call your C function

    What this is in effect doing is “freeing” the portion of your stack that the segment registers were stored in. In fact you don’t even need to worry about the C function at all because the return address gets pushed onto the stack at the call instruction and blows away your record of ds and es before you even get to the C-call. When you get back from the C call, you try to tidy up the callstack, but you don’t get it quite right – partly because you’ve already messed it up, and partly because you don’t clean up the stack after the function call (assuming that handler uses the _cdecl calling convention).

    This causes you to pop a bogus value for ds. When you load that into ds the CPU checks the value against the GDT, and finds that it is invalid. At this point it raises a GPF (exception 13) which you are seing. That, to a certain extent, recovers the stack (the CPU is looking after the SS for you) and leaves the old value of ds set – so you never actually change ds, which allows you to run printf again.

    You need to be much more careful about aligning the stack, and whenever you add to the stack pointer as you do, you need to consider the data that was in that range gone forever because either the next guy, or possibly an unexpected interrupt, is going to trip you up.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.