Click here to Skip to main content
13,150,622 members (44,399 online)
Click here to Skip to main content
Add your own
alternative version


83 bookmarked
Posted 2 Sep 2007

Real time. Windows Runs in the Ring2 (x86 32 bit OS)

, 3 Dec 2014
Rate this:
Please Sign up or sign in to vote.
This article presents the Hadcon's real time module for Windows 7/XP


For a variety of business and technical reasons, Microsoft Windows Vista, XP/2000 operating systems are increasingly being considered as a platform for deployment of real-time systems. The reasons include:

  1. The many applications available on the platform
  2. The variety of development tools available on the platform
  3. The richness of the Microsoft Win32 application programming interface (API)
  4. The large number of developers, support personnel, and end users who are familiar with the system

The Hadcon's real time module (RTM) has been developed to bring "hard" real time capabilities (such as creation of timers, system thread, of management by resources – memory, ports, interrupts) to Windows, excepting necessity of creating of the driver.

RTM Architecture

Precise execution of events is critical in a real-time system. RTM provides complete flexibility to the developer to determine the appropriate timer resolution for their system. Timer interval values can be set between 100 microsecond and 1 millisecond (100,200,500,1000). The timer interval is default inside of [AddNewInst] section rtmdrv.inf file's HKR,,TimerQuantum,,500.

RTM uses the "mixed" code, i.e., timer's, interrupt's routines (working in Ring 0) and user routines (working in Ring 3) are incorporated in one DLL module.
Real-time components (timer, system thread and interrupt routines) must be locked in memory to avoid latencies due to page faults. Therefore RTM defines its own sections for location of a code, data and stack using the data_seg, code_seg, and bss_seg pragmas.

#pragma code_seg( "RTMCODE ")

Specifies a code section where functions are to be allocated.
For example:

#pragma code_seg( "RTMCODE ")

void timer1(unsigned dwmess )

#pragma code_seg()

#pragma data_seg( "RTMDATA ")

Specifies a code section where initialized data are to be allocated.
For example:

#pragma data_seg( "RTMDATA ")

char buff[10] = {0};
int k1 = 0;

#pragma data_seg()

#pragma bss_seg( "RTMBSS ")

char buff[10];
int k1;

#pragma bss_seg()


RTM uses the system timer. The system timer generates system ticks at a fixed rate of one tick per millisecond (or 100, 200 ,500 microsecond), which is the rate at which a timer interrupt is generated and serviced by the operating system (interrupt service routine ISR).
RTmCreateTimer function lets you attach a handling routine to an timer interrupt, much the same way that the RTM timer routines allow you to associate a handling routine with a given timer expiration.
RTM timers are not synchronization objects which means threads cannot wait for single objects with an RTM timer handle.

This is in contrast to the Windows waitable timer, which is an object on which a thread can wait, or against which a thread can receive notification.

Example Code

#pragma code_seg( "RTMCODE ")
void timer1(unsigned dwmess )
 int i;
 unsigned nstate;
 unsigned nvalue;
 unsigned short ndigport;

      if(nstop ) return ;

      if(initLpt == 0)
       _asm {
         mov dx,378h
         mov ax,0
         out dx,ax ;//outp(BASE_LPT,0);
         add dx,2; CR
         mov ax,2eh
         out dx,ax ;//outp(BASE_LPT+CR,0x2e);
      initLpt =1;



#pragma code_seg()

void func()
  int error;
  HANDLE tm;

       if(RtmInit(hRTMDLL,KERNEL_MODE) == -1) return 0;

         RtmDebug(hRTMDLL ,TRUE);
         tm = RtmCreateTimer
            (hRTMDLL ,timer_anl,0x2000,stanl.quantum,KERNEL_MODE);
         RtmStartTimer(hRTMDLL ,tm,0x3000);
         error = RtmTimerPostMessage(hRTMDLL ,tm,0x100);

Using Threads

The RtmCreateThread function creates a system thread (See: PsCreateSystemThread in the DDK for Windows XP/2000).
You can set the priority value for the specified thread using KeSetPriorityThread and KeSetBasePriorityThread functions.
Inside of the body thread function, we can use majority DDK functions.
RTM thread defines own sections for location of a code.

#pragma code_seg( "RTMTHR ")
// thread code
#pragma code_seg()

Example Code

#pragma code_seg( "RTMTHR ")
void writetofile(PWSTR pfl,UINT addr,int nsize)
   OBJECT_ATTRIBUTES objectAttributes;
   HANDLE FileHandle = NULL;
   UNICODE_STRING fileName1;
   NTSTATUS status;

        fileName1.Buffer = NULL;
        fileName1.Length = 0;
        fileName1.MaximumLength = 256;

        fileName1.Buffer = (unsigned short *)ExAllocatePool(PagedPool,


        RtlZeroMemory(fileName1.Buffer, fileName1.MaximumLength);
        status = RtlAppendUnicodeToString(&fileName1, pfl);
        InitializeObjectAttributes (&objectAttributes,
        NULL );

       status = ZwCreateFile(&FileHandle,
        0 );


         (void *)addr,
         NULL );

        DbgPrint ("Close file");


int systhread(void *param)
 unsigned int _cr3 = 0;
 PVOID pv;

       if(n_count_task == 10)
        _asm mov eax,cr3
        _asm mov _cr3,eax

        pf.HighPart = 0;
        pf.LowPart = _cr3;
        pv = MmGetVirtualForPhysical (pf);
        writetofile(L"\\??\\C:\\tmp\\qqq1",(UINT)pv ,0x1000);

       return 0;

#pragma code_seg()

void func()
 int error;

        if(RtmInit(hRTMDLL,KERNEL_MODE) == -1) return 0;

        th = RtmCreateThread(hRTMDLL ,(THREAD_START_ROUTINE)systhread,NULL);


RTM allows a process to directly interface with a device without having to write a Windows driver via RtmConnectInterrupt or RtmHookInterrupt functions. A process can attach to an interrupt handler using RtmConnectInterrupt function (See: HalGetInterruptVector in the DDK for Windows XP/2000).
The RtmHookInterrupt function allows to hook an interrupt.

typedef struct _tagRTMINTERUPT {
  ULONG nVector ;
  ULONG BusNumber ;
  unsigned char bShared;
  void *lpParameter;


Example Code

#pragma data_seg( "RTMDATA ")
int _isr = 0;
#pragma data_seg()
void newintr(void *pv)
int func1()
 HANDLE tm; int count;
 HANDLE pi,pi1;

      if(RtmInit(hRTMDLL, KERNEL_MODE) == -1) return 0;

         cinter.BusNumber = 1;
         cinter.DevID = 0x2f00;
         cinter.nVector =0;
         cinter.VenID = 0x14f1;
         cinter.itype = PCIBus;
         cinter.lpParameter = NULL;
         cinter.newintr = newintr;

         pi = RtmConnectInterrupt(hRTMDLL ,&cinter);
         cinter.BusNumber = 0;
         cinter.nVector =1;
         cinter.itype = Isa;
         cinter.lpParameter = NULL;
         cinter.newintr = newintr;

         pi1 = RtmHookInterrupt (hRTMDLL ,&cinter);
         count = _isr;
         count = _isr;
         count = _isr;

         RtmUnhookInterrupt (hRTMDLL ,pi1);



This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author
Russian Federation Russian Federation
No Biography provided

You may also be interested in...

Comments and Discussions

Generalvisual studio 2005 LNK1104 LIBC.LIB not found Pin
devCoder30-May-09 2:43
memberdevCoder30-May-09 2:43 
GeneralIOPL_x macro or const not defined Pin
devCoder30-May-09 3:52
memberdevCoder30-May-09 3:52 
Generaldemo does not work-and missing function exporting from dll Pin
devCoder30-May-09 22:26
memberdevCoder30-May-09 22:26 
Generaldevioctl.h Pin
smart_dummies12-Mar-09 21:54
membersmart_dummies12-Mar-09 21:54 
GeneralRe: devioctl.h Pin
khavkin@sandy.ru12-Mar-09 23:19
memberkhavkin@sandy.ru12-Mar-09 23:19 
GeneralRe: devioctl.h Pin
smart_dummies13-Mar-09 2:45
membersmart_dummies13-Mar-09 2:45 
GeneralHardware RealTime Pin
Jarin6-Jan-09 11:55
memberJarin6-Jan-09 11:55 
General[Message Deleted] Pin
Mikael Sundfors25-Nov-08 11:49
memberMikael Sundfors25-Nov-08 11:49 
GeneralRe: My vote of 1 Pin
khavkin@sandy.ru26-Nov-08 1:58
memberkhavkin@sandy.ru26-Nov-08 1:58 
GeneralDid not heal me Pin
NGS 54967225-Nov-08 5:51
memberNGS 54967225-Nov-08 5:51 
Generallib fiels Pin
JimmyO16-Dec-07 23:55
memberJimmyO16-Dec-07 23:55 
QuestionWhere is the source code ? Pin
Bruce Zhao30-Sep-07 3:14
memberBruce Zhao30-Sep-07 3:14 
Generaluseless & unstable Pin
hledejto17-Sep-07 21:38
memberhledejto17-Sep-07 21:38 
GeneralRe: useless & unstable Pin
khavkin@sandy.ru17-Sep-07 23:34
memberkhavkin@sandy.ru17-Sep-07 23:34 
GeneralRe: useless & unstable Pin
khavkin@sandy.ru26-Sep-07 1:05
memberkhavkin@sandy.ru26-Sep-07 1:05 
QuestionRealtime applications? Pin
Shawn Poulson4-Sep-07 2:40
memberShawn Poulson4-Sep-07 2:40 
QuestionHard job to do this..... Pin
kennychou3-Sep-07 23:15
memberkennychou3-Sep-07 23:15 
GeneralCompilation Error Pin
Vep3-Sep-07 21:51
memberVep3-Sep-07 21:51 
GeneralRe: Compilation Error Pin
khavkin@sandy.ru3-Sep-07 22:44
memberkhavkin@sandy.ru3-Sep-07 22:44 
GeneralRe: Compilation Error Pin
khavkin@sandy.ru26-Sep-07 1:01
memberkhavkin@sandy.ru26-Sep-07 1:01 
GeneralNaive..... Pin
Anton Bassov3-Sep-07 13:00
memberAnton Bassov3-Sep-07 13:00 
GeneralRe: Naive..... Pin
khavkin@sandy.ru3-Sep-07 22:40
memberkhavkin@sandy.ru3-Sep-07 22:40 
GeneralRe: Naive..... [modified] Pin
Valery A. Boronin6-Sep-07 0:35
memberValery A. Boronin6-Sep-07 0:35 
GeneralRe: Naive..... Pin
khavkin@sandy.ru6-Sep-07 2:24
memberkhavkin@sandy.ru6-Sep-07 2:24 
GeneralRe: Naive..... Pin
Anton Bassov7-Sep-07 8:23
memberAnton Bassov7-Sep-07 8:23 
GeneralRe: Naive..... Pin
khavkin@sandy.ru9-Sep-07 1:19
memberkhavkin@sandy.ru9-Sep-07 1:19 
GeneralRe: Naive..... Pin
hledejto17-Sep-07 9:16
memberhledejto17-Sep-07 9:16 
GeneralSystem Management Mode Pin
Valery A. Boronin19-Sep-07 7:01
memberValery A. Boronin19-Sep-07 7:01 
GeneralVery interesting! Pin
WREY3-Sep-07 7:04
memberWREY3-Sep-07 7:04 
GeneralInteresting Pin
Jerry Evans3-Sep-07 3:11
memberJerry Evans3-Sep-07 3:11 
GeneralRe: Interesting Pin
khavkin@sandy.ru3-Sep-07 6:06
memberkhavkin@sandy.ru3-Sep-07 6:06 
GeneralRe: Interesting Pin
mjtooba27-Mar-10 10:02
membermjtooba27-Mar-10 10:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170924.2 | Last Updated 3 Dec 2014
Article Copyright 2007 by
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid