How to set up interrupts?

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
Klakap
Member
Member
Posts: 299
Joined: Sat Mar 10, 2018 10:16 am

How to set up interrupts?

Post by Klakap »

Good day!
Already long time I try to give into your operating system interrupts but I can't get it. I tried different tutorials(for example, Bran's Kernel Development Tutorial) but without success. Please give me a code which I will be sufficient to copy them to my operating system and using it I interrupts will work.
alexfru
Member
Member
Posts: 1112
Joined: Tue Mar 04, 2014 5:27 am

Re: How to set up interrupts?

Post by alexfru »

:lol: Unfortunately for you it doesn't work that way. You need to learn and understand how interrupt handling works and fits into the picture. Chances of copied low-level system code working as-is are close to zero.
Klakap
Member
Member
Posts: 299
Joined: Sat Mar 10, 2018 10:16 am

Re: How to set up interrupts?

Post by Klakap »

I already understand how interrupts work. Your operating system I am writing in the language C and assembler. Seemed to me a reasonable method irq_install_handler from Bran's Kernel Development Tutorial but when I wanted to compile(I made the edits to it according to the https://wiki.osdev.org/Bran%27s_Known_Bugs) but I got this result :( :
In file included from kernel.c:6:0:
system.h:22:14: warning: conflicting types for built-in function ‘memset’
extern void *memset(void *dest, char val, size_t count);
^
kasm.about: In function `gdt_flush':
kernel.asm:(.text+0x32): undefined reference to `gp'
kasm.about: In function `idt_load':
kernel.asm:(.text+0x4f): undefined reference to `idtp'
kasm.about: In function `isr_common_stub':
kernel.asm:(.text+0x166): undefined reference to `fault_handler'
kasm.about: In function `irq_common_stub':
kernel.asm:(.text+0x1ff): undefined reference to `irq_handler'
kc.about: In function `main':
kernel.c:(.text+0x114): undefined reference to `gdt_install'
kernel.c:(.text+0x119): undefined reference to `idt_install'
kernel.c:(.text+0x11e): undefined reference to `isrs_install'
kernel.c:(.text+0x123): undefined reference to `irq_install'
kernel.c:(.text+0x128): undefined reference to `init_video'
kernel.c:(.text+0x12d): undefined reference to `timer_install'
kernel.c:(.text+0x132): undefined reference to `keyboard_install'
kernel.c:(.text+0x140): undefined reference to `puts'

How do I modify the code to give me the compile was carried out without errors and in order for me to work method irq_install_handler?

//edit:
Still I want to ask how can I from assembler to call the method which I write in C. When I tried I got the undefined reference to "my methods," What do I with it do?
hexcoder
Posts: 15
Joined: Thu Oct 20, 2016 12:26 pm

Re: How to set up interrupts?

Post by hexcoder »

None of those errors are problems that are unique to OSDev. If you knew C, you would be able to fix them yourself.

https://wiki.osdev.org/Beginner_Mistakes#A_Hard_Truth
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: How to set up interrupts?

Post by Schol-R-LEA »

Actually, the first one, the conflict on the function name memset(), is one which is pretty uncommon outside of OS-dev. The others, however, you are correct about.

That specific problem relates to the use of a stock, host-targeted compiler configuration without the --fnobuiltins option (or --ffreestanding, which includes the no-builtins effect and is the recommended usage), rather than a cross-compiler and a Makefile with the appropriate settings baked in.

The crux of the matter is that GCC will automagically inline certain standard functions, including memset(), unless told otherwise. The code it generates for them isn't from a library, but built into GCC's code generators themselves. Now, not all GCC backends have the same built-ins, but most will include a suite of 'functions' that aren't actually implemented as functions.

The problem arises from the fact that the backend implementations have to be aware of the target environment to some degree (in order to know which type of Object Files to generate and so forth. While most GCC installations include several different backends to facilitate cross-development, using them with an out-of-the-box configuration for your host platform means applying options, having different linker output, and more; for OS development, this quickly becomes impractical. This is why we alway recommend configuring a GCC Cross Compiler and a OS Specific Toolchain which avoids both ambiguity and repetition.

I recommend that you reference the GCC documentation for the version you are using (the current version is 7.3.0, but there is a high likelihood that you are using an older version, so make sure you read the correct one). This Guide to Bare Metal Programming with GCC would be useful to review as well, though you will want to keep in mind that it is mainly aimed at writing standalone programs on the Raspberry Pi rather than on PC operating system development.

Now, normally I would say that it would help us if you could post more details about your development environment, things like what kind of host system (hardware and OS) you are using, what emulator or virtualizer you mean to run the test OS in, what versions of GCC and binutils you are working with, and so forth, but this may be premature.

Hexcoder's basic point is correct: most of those errors are ones which can come up in any C programming project, and the fact that you don't know how to address them indicates that you don't have enough general programming experience to tackle OS dev. I strongly recommend shelving this project for a few months, or better still a few years, while you build your general programming skills up.

I will add one more piece of advice which too many others have ignored: if you haven't set up version control (such as Subversion, Git, or Mercurial) for your software projects, with an offsite repository (on a free host such as Github or CloudForge), drop everything and do that now for every project you intend to work on. We cannot stress the importance of this enough. Sooner or later, you will make a terrible mistake that wrecks the code you are working on; sooner or later, you will have a hardware failure that has the same result. This happens to every programmer, without exceptions, and the best protection you have for that is offsite version control. It is more than just a backup system, however; it gives you a record of your work, and makes it easy to collaborate with others on a project. You owe it to yourself to do this, even on 'little projects' that you don't expect to be important - little projects have a way of becoming big ones, of which two prime examples are the Linux OS and the Perl programming language.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Klakap
Member
Member
Posts: 299
Joined: Sat Mar 10, 2018 10:16 am

Re: How to set up interrupts?

Post by Klakap »

Me not the main method *memset but I want to know how can I from assembler to call the method which I write in C.
version of gcc:
5.4.0
to test the operating system I'm using QEMU 2.5.0.

I am at the very beginning of doing my operating system, so in it I don't have anything(so far I have experimented with the statements on the screen in text mode). Therefore, I thought that once niekdo solved how can I set the interruption so give me a specific code. The main problem I have with it is that when in the assembler I will write:
extern irq_handler
call irq_handler
so the linker will write undefined reference to `irq_handler' . Even more I'm confused that this bug writes only sometimes.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to set up interrupts?

Post by iansjack »

Again, if you don't know how to call a C function from assembler, or vice versa, you really are not ready for OS development. Develop your programming skills on simpler programs before tackling this challenging task.
Klakap
Member
Member
Posts: 299
Joined: Sat Mar 10, 2018 10:16 am

Re: How to set up interrupts?

Post by Klakap »

But I don't know from where should I learn as of the asembler to call the method from the language C. Please explain it to me or me to encourage a web page where I can learn.
User avatar
iansjack
Member
Member
Posts: 4706
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: How to set up interrupts?

Post by iansjack »

This site isn't really intended to teach programming.
User avatar
Schol-R-LEA
Member
Member
Posts: 1925
Joined: Fri Oct 27, 2006 9:42 am
Location: Athens, GA, USA

Re: How to set up interrupts?

Post by Schol-R-LEA »

Klakap wrote:But I don't know from where should I learn as of the asembler to call the method from the language C. Please explain it to me or me to encourage a web page where I can learn.
Sure, why not. Let Me Google That For You, as you don't seem to have done it yourself yet.

A very, very brief search of the wiki would have given you the page on Calling Conventions as well. Admittedly, you may simply not have known that term, and so didn't know to search the wiki for it, but if so, then that too points to a lack of the necessary experience. Basically, though, the question boils down to, "how do I call an assembly function from C, or a C function from assembly", and the answer is, it depends on the way arguments are passed between functions, which is what is called a 'calling convention'. There are a number of them for C on the x86, so you may need to pick one.

Oh, and you need to declare the assembly language function label as global in the assembly code, and have an extern declaration in the C code, in order to call an assembly function from C. To call C functions from assembly, you need to have an extern declaration of the function name in the assembly code calling it. Those are needed in order to allow the linker to find the functions and link them together correctly.

As we've stated repeatedly, this is something that you should have known before jumping into OS development. It is not necessarily common knowledge for application programmers, but anyone who has done any systems programming (or any mixed-language programming, really) should have at least some familiarity with doing this.

There is no shame or harm in putting off something you aren't ready for. If you spend some more time studying and practicing, and apply yourself to getting the basics down pat, you'll be able to come back to this project eventually. Both patience and impatience are virtues for programmers, but you need to have the right sort of each at the right time, and for you right now, trying to dive into OS development without more study is the wrong sort of impatience.

Trust me, I've been there, too. I think it is safe to say that all of the regulars on the group have, at one time or another.

However, it is also true that by committing to one of the most difficult challenges any programmer can face, you are setting a certain expectation of excellence. The regulars of the forum have high expectations and higher standards, so you need to be aware of that. If you can step up your game, we will gladly help you, but we expect you to do your due diligence first.

You might want to look at the page on Inline Assembly as well, even though that wasn't what you were asking for (or at least shouldn't have been, since using inline assembly for interrupt routines is a Bad Idea), as it would answer several other questions you might have. You may need to reference the page explaining how the Opcode Syntax of the GAS assembler differs from that of the Intel assemblers such as NASM, though it shouldn't be necessary in order to understand the examples given in the other pages. I would also recommend reviewing the pages on Linkers, Linker Scripts, Execution Loaders, Object Files, ELF, Executable Formats, and similar topics, to get a deeper understanding of what is happening and why.
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.
Klakap
Member
Member
Posts: 299
Joined: Sat Mar 10, 2018 10:16 am

Re: How to set up interrupts?

Post by Klakap »

Thank you, I managed to get from the assembler to call the method in C.
Post Reply