5

I am writing a simple code in c on a Linux(A13-OLinuXino-MICRO) with A13 processor. I am using gcc compiler if that's important. Let's say I have a simple loop that is just counting from 1 to 100 and I want to have as less as possible interruption in this loop. I assume the best that I can do is disabling interrupts(even the timer interrupt if possible) and enabling it after the loop. I have multiple places in my program that I need to enforce this. Can anybody help me with a simple C code that a loop is protected from interruption...something that I can compile and run on my platform?

EDIT: Kernel thread might be an answer. How can I run something with kernel privilege? I see people talk about disable_local_interrupt() but I don't know how to use it.

EDIT: This is the actual problem that I am trying to solve. I need to run my applications in a interrupt-free environment.

EDIT: Yes I am sure that Linux is what I need to use and I know that interrupts are important for OS and that's why I don't want to disable them forever. I just want to enforce no-interrupt for a fraction of second on OS when I am running this. Also, There is no main problem that I need to solve, this is "the problem" that I need to solve, so stop asking why I want to do this.

MSalters
  • 173,980
  • 10
  • 155
  • 350
Nhome
  • 303
  • 2
  • 3
  • 12
  • 8
    There hopefully is no way to do this! You should not even think about doing modifications at such a low-level from user code (and be very careful in kernel code). This is an XY problem. What is your **actual** problem you try to solve by this? – too honest for this site Jan 25 '17 at 18:34
  • 1
    If that was a generally-available capability, anyone could write a program with a never-ending loop that would block the entire system until someone physically pulled the plug, because task scheduling relies on clock-generated interrupts. – zneak Jan 25 '17 at 18:40
  • 1
    Disabling interrupt is as simple as processor instruction. Usually the mnemonic is something similar to `cli`. However, it is priviliged instruction, and can only be executed in a priviliged context. – SergeyA Jan 25 '17 at 18:42
  • this is the actual problem that I want to solve, I need to run my application interrupt-free. if it's not possible to disable interrupts from inside applications, how can I do it from outside and run entire application without interruption? – Nhome Jan 25 '17 at 18:42
  • 1
    Have you considered that Linux might not be the right tool for what you're trying to do? I'm not sure that Linux can tolerate disabled interrupts for a very long time, or that user-mode programs are meant to run in an interrupt-disabled environment at all. There are a lot of things that you won't be able to do without interrupts, like reading data from a hard drive or similar device, or using the network. – zneak Jan 25 '17 at 18:47
  • 1
    Another possibility for you would be to write a kernel module and run all of your code from a kernel thread. I'm still not sure that Linux is what you'll be looking for, though. – zneak Jan 25 '17 at 18:51
  • 2
    Use a realtime operating system. – that other guy Jan 25 '17 at 18:52
  • 3
    "I need to run my applications in a interrupt-free environment" -- then you don't want to run it on Linux. Nor on Windows, or OS X, or any other multitasking operating system. If you need to be certain that your program is not interrupted then you probably need to run it as a freestanding program on the bare metal. – John Bollinger Jan 25 '17 at 18:53
  • What is the reason exactly of running application on Linux in "interrupt free environment?" – Long Smith Jan 25 '17 at 18:58
  • @JohnBollinger, why? Op could easily do a kernel driver and run the program of it uninterrupted if so OP wishes. – SergeyA Jan 25 '17 at 18:59
  • 1
    using a kernel thread sounds pretty good, I see people talk about disable_local_interrupt() but I don't know how to use it. – Nhome Jan 25 '17 at 19:57
  • @Nhome you are being extremely rude by refusing to explain why you want to disable interrupts. Saying "I want to disable interrupts in a userland process" strongly suggests that you are confused about what you are trying to achieve and hence the questions you refuse to ask are more than valid. – MK. Jan 25 '17 at 20:13
  • Oh I'm sorry I am not trying to be rude, it is really what I need to do, I am doing some research on side-channel signal and this is an experiment that I need to get its signal. It is getting signal of executing this application with as less as possible interruption. – Nhome Jan 25 '17 at 20:19
  • Well, on a non-realtime preemptive multitasking OS like a stock Linux kernel, your side channel signal is going to be very noisy, especially if it's connected to a network. – bazza Jan 25 '17 at 20:30
  • Can someone just answer the OPs question by demonstrating a full, runnable kernel space loop and code sample? I haven't the faintest idea how to write Linux code in kernel space but am interested to learn. – Gabriel Staples May 26 '19 at 01:23

3 Answers3

9

Userspace cannot disable or enable interrupts.

doron
  • 27,972
  • 12
  • 65
  • 103
  • Can you demo how to enter kernel space and do it then? – Gabriel Staples May 26 '19 at 01:25
  • @GabrielStaples write a kernel module and disable interrupts. – sevko Dec 03 '19 at 22:29
  • 1
    @sevko, you just reworded my request. Me: "doron, can you demo how to enter kernel space and do it then?". You: "[In other words doron, can you please demo how to] write a kernel model and disable interrupts?" – Gabriel Staples Dec 04 '19 at 04:15
  • 1
    Take a look at implementing a char device. There are examples on the internet. Once in kernel mode, there is a simple API to disable interrupts. Just note, you cannot return to userspace before interrupts are re-enabled – doron Dec 04 '19 at 17:16
0

According to ARM.com http://infocenter.arm.com/help/index.jsp?topic=%2Fcom.arm.doc.ddi0344k%2FBeidabib.html this CPU does have different operating modes, which suggests that you probably can't disable interrupts from a user process.

MK.
  • 33,605
  • 18
  • 74
  • 111
  • well then is there any possible way to do it with a higher privilege? – Nhome Jan 25 '17 at 20:23
  • 1
    yes, run your code in the kernel. By writing a kernel driver. I am no kernel developer, but I'm going to guess that you don't want to have a way to disable and then enable interrupts from the userland; you should run your entire operation in kernel mode: disable interrupts, do whatever it is you want, re-enabled interrupts. This still sounds weird and a RTOS is most likely a better choice. – MK. Jan 25 '17 at 20:28
0

I think @doron is right, you can't disable/enable interrupt in user mode, but you have a work around. You can write a driver which encapsulates the "loop" code in an interface, and you can enable/disable interrupt in your driver. Your application just need to call that interface.

Jun Ge
  • 408
  • 4
  • 13