xenomai内核解析--双核系统调用(三)--如何为xenomai添加一个系统调用

简介: 本文介绍了如何在Xenomai中添加自定义系统调用`get_timer_hits()`,该调用用于获取CPU定时器中断次数。首先,在`syscall.h`中定义127号系统调用,并在`clock.h`和`clock.c`中声明和实现该函数。接着,更新libcobalt库以包含新接口。最后,提供了一个示例应用,演示如何在实时任务中使用此系统调用。编译内核和库后,运行示例程序显示定时器中断次数,体现了Xenomai的tickless特性。

版权声明:本文为本文为博主原创文章,转载请注明出处。如有错误,欢迎指正。
@[toc]

一、添加系统调用

下面给xenomai添加一个系统调用get_timer_hits(),用于获取应用程序运行CPU的定时器中断产生的次数,类似于VxWorks里的tickGet()。需要说明一下VxWorks是采用周期tick的方式来驱动系统运作,tickGet()获取的也就是tick定时器中断的次数,但xenomai使用的tickless,即定时器不是周期产生tick的。所以get_timer_hits()用于获取定时器中断次数,get_timer_hits()没有具体用途,这里主要用来举例怎么为xenomai添加一个实时系统调用。

在前两篇文中说到,xenomai每个系统的系统系统调用号在\cobalt\uapi\syscall.h中:

#define sc_cobalt_bind                0
#define sc_cobalt_thread_create            1
#define sc_cobalt_thread_getpid            2
    ......
#define sc_cobalt_extend            96

在此添加sc_cobalt_get_timer_hits的系统,为了避免与xenomai系统调用冲突(xenomai官方添加的系统调用号从小到大),那我们就从最后一个系统调用添加,即127号系统调用,如下。

#define sc_cobalt_bind                0
#define sc_cobalt_thread_create            1
#define sc_cobalt_thread_getpid            2
    ......
#define sc_cobalt_extend            96
#define sc_cobalt_ftrace_puts                   97
#define sc_cobalt_recvmmsg                      98
#define sc_cobalt_sendmmsg                      99
#define sc_cobalt_clock_adjtime                 100
#define sc_cobalt_thread_setschedprio           101


#define sc_cobalt_get_timer_hits            127
#define __NR_COBALT_SYSCALLS                    128 /* Power of 2 */

先确定一下我们这个函数的API形式,由于是一个非标准的形式,这里表示如下:

int get_timer_hits(unsigned long *u_tick);

参数为保存hits的变量地址;

返回值:成功0;出错 <0;

系统调用的头文件,然后添加一个系统调用的声明,觉得它和clock相关,那就放在kernel\xenomai\posix\clock.h中吧。

#include <linux/ipipe_tickdev.h>

COBALT_SYSCALL_DECL(get_timer_hits,
           (unsigned long __user *u_tick));

然后是该函数的内核实现,放在/kernel\xenomai\posix\clock.c,如下:

COBALT_SYSCALL(get_timer_hits, primary,
           (unsigned long __user *u_tick))
{
    struct xnthread *thread;
    unsigned long tick;
    int cpu;
    int ret = 0;
    unsigned int irq;

    thread = xnthread_current();
    if (thread == NULL)
        return -EPERM;

    /*得到当前任务CPU号*/
    cpu = xnsched_cpu(thread->sched);

    irq = per_cpu(ipipe_percpu.hrtimer_irq, cpu);    
    /*读取该CPU中断计数*/
    tick = __ipipe_cpudata_irq_hits(&xnsched_realtime_domain, cpu,
                                irq);
    if (cobalt_copy_to_user(u_tick, &tick, sizeof(tick)))
        return -EFAULT;

    return ret;
}

需要注意的是该系统调用的权限,这里使用primary,表示只有cobalt上下文(实时线程)才能调用。

修改完成后重新编译内核并安装。

二、Cobalt库添加接口

在前两篇文中说到,xenomai系统调用由libcobalt发起,所以修改应用库来添加该函数接口,添加声明include\cobalt\time.h

COBALT_DECL(int, get_timer_hits(unsigned long tick));

xenomai3.x.x\lib\cobalt\clock.c添加该接口定义:

COBALT_IMPL(int, get_timer_hits, (unsigned long * tick))
{
        int ret;

        ret = -XENOMAI_SYSCALL1(sc_cobalt_get_tick,
                                tick);

        return ret;
}

重新编译并安装xenomai库,详见本博客其他文章。

三、应用使用

由于我们添加get_timer_hits()系统调用时,指定了系统调用的权限为primary,这里创建一个实时任务,使用宏__RT()指定链接到libcobalt库。

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sched.h>
#include <time.h>
#include <unistd.h>
#include <signal.h>
#include <alchemy/task.h>
#include <alchemy/timer.h>
#include <alchemy/sem.h>
#include <boilerplate/trace.h>
#include <xenomai/init.h>

#define PRIO 50

void test(void *cookie)
{
    unsigned long tick;
    int ret;
    ret  = __RT(get_timer_hits(&tick));
    if (ret){
        fprintf(stderr,
            "%s: failed to get_tick,%s\n",
            __func__,strerror(-ret));
        return ret;
    }    
    fprintf(stdout,"timer_hits:%ld\n",tick);
    /*....*/
    return 0;
}

int main(int argc, char *const *argv)
{
    struct sigaction sa __attribute__((unused));
    int sig, cpu = 0;
    char sem_name[16];
    sigset_t mask;
    RT_TASK task;
    int ret;

    sigemptyset(&mask);
    sigaddset(&mask, SIGINT);
    sigaddset(&mask, SIGTERM);
    sigaddset(&mask, SIGHUP);
    sigaddset(&mask, SIGALRM);
    pthread_sigmask(SIG_BLOCK, &mask, NULL);
    setlinebuf(stdout);

    ret = rt_task_spawn(&task, "test_task", 0, PRIO, 
                        T_JOINABLE, test, NULL);
    if (ret){
        fprintf(stderr,
            "%s: failed to create task,%s\n",
            __func__,strerror(-ret));
        return ret;
    }

    __STD(sigwait(&mask, &sig));
    rt_task_join(&task);
    rt_task_delete(&task);

    return 0;
}

编译Makefile:

XENO_CONFIG := /usr/xenomai/bin/xeno-config

PROJPATH = .

CFLAGS := $(shell $(XENO_CONFIG)   --posix --alchemy --cflags)
LDFLAGS := $(shell $(XENO_CONFIG)  --posix --alchemy --ldflags)
INCFLAGS= -I$(PROJPATH)/include/


EXECUTABLE := get-timer-hits

src = $(wildcard ./*.c)
obj = $(patsubst %.c, %.o, $(src))

all: $(EXECUTABLE)

$(EXECUTABLE): $(obj)
        $(CC) -g -o $@ $^  $(INCFLAGS) $(CFLAGS) $(LDFLAGS)

%.o:%.c
        $(CC) -g -o $@ -c $<  $(INCFLAGS) $(CFLAGS) $(LDFLAGS)

.PHONY: clean
clean:
        rm -f $(EXECUTABLE) $(obj)

运行结果:

$./get-timer-hits
timer_hits:3

可以看到,虽然系统已经启动十几分钟了,但一直没有运行xenomai应用,xenomai tick相关中断才产生了3次,这就是tickless,后面会出xenomai调度及时间子系统相关文章,敬请关注。

目录
相关文章
|
5天前
|
Linux 编译器 调度
xenomai内核解析--双核系统调用(二)--应用如何区分xenomai/linux系统调用或服务
本文介绍了如何将POSIX应用程序编译为在Xenomai实时内核上运行的程序。
36 1
xenomai内核解析--双核系统调用(二)--应用如何区分xenomai/linux系统调用或服务
|
5天前
|
存储 Linux API
xenomai内核解析--双核系统调用(一)
本文介绍了Xenomai内核系统调用的实现,探讨了在Linux内核与Xenomai实时内核共存时,系统调用如何区分和交互。系统调用是用户空间与内核空间通信的关键,它提供了硬件抽象、系统稳定性、安全性和可移植性。在32位系统中,通过`int 0x80`指令触发,而在64位系统中,使用`syscall`指令。Xenomai通过I-pipe拦截系统调用,区分实时与非实时任务,并通过`cobalt_syscalls`表执行相应操作。文章还详细解析了系统调用表的生成和权限控制机制。
30 1
xenomai内核解析--双核系统调用(一)
|
5天前
|
消息中间件 调度 数据安全/隐私保护
xenomai内核解析--任务同步互斥机制(一)--优先级倒置
本文是关于Xenomai实时操作系统中资源管理和优先级倒置问题的概述。Xenomai使用`xnobject`和`xnregistry`管理任务间的同步互斥资源,如信号量、互斥锁等。资源管理涉及访问控制和资源保存,确保共享资源的正确调度。文章还介绍了优先级倒置现象,即高优先级任务因低优先级任务持有资源而被阻塞。为解决此问题,Xenomai采用了优先级继承策略,临时提升低优先级任务的优先级,以防止持续的优先级反转。文章后续将深入分析`xnsynch`模块和优先级倒置解决方案。
26 1
xenomai内核解析--任务同步互斥机制(一)--优先级倒置
|
3天前
|
Linux 网络安全 Windows
网络安全笔记-day8,DHCP部署_dhcp搭建部署,源码解析
网络安全笔记-day8,DHCP部署_dhcp搭建部署,源码解析
|
4天前
HuggingFace Tranformers 源码解析(4)
HuggingFace Tranformers 源码解析
6 0
|
4天前
HuggingFace Tranformers 源码解析(3)
HuggingFace Tranformers 源码解析
7 0
|
4天前
|
开发工具 git
HuggingFace Tranformers 源码解析(2)
HuggingFace Tranformers 源码解析
7 0
|
4天前
|
并行计算
HuggingFace Tranformers 源码解析(1)
HuggingFace Tranformers 源码解析
9 0
|
5天前
PandasTA 源码解析(二十三)
PandasTA 源码解析(二十三)
42 0
|
5天前
PandasTA 源码解析(二十二)(3)
PandasTA 源码解析(二十二)
34 0

热门文章

最新文章

推荐镜像

更多