From owner-freebsd-hackers@FreeBSD.ORG Mon Oct 22 20:30:34 2007 Return-Path: Delivered-To: FreeBSD-Hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 900DD16A477 for ; Mon, 22 Oct 2007 20:30:34 +0000 (UTC) (envelope-from Danovitsch@vitsch.net) Received: from VM01.Vitsch.net (vm01.vitsch.net [85.17.51.140]) by mx1.freebsd.org (Postfix) with ESMTP id D57A813C4C2 for ; Mon, 22 Oct 2007 20:30:33 +0000 (UTC) (envelope-from Danovitsch@vitsch.net) Received: from Tuinhuisje.Vitsch.net ([217.166.176.2]) by VM01.Vitsch.net (8.13.8/8.13.8) with ESMTP id l9MKAi8h058732 for ; Mon, 22 Oct 2007 22:10:45 +0200 (CEST) (envelope-from Danovitsch@vitsch.net) Received: from [192.168.46.137] (dhcp-077-248-238-249.chello.nl [77.248.238.249]) (authenticated bits=0) by Tuinhuisje.Vitsch.net (8.13.1/8.13.1) with ESMTP id l9MKC36F013098 for ; Mon, 22 Oct 2007 22:12:03 +0200 (CEST) (envelope-from Danovitsch@vitsch.net) From: "Daan Vreeken [PA4DAN]" Organization: Vitsch Electronics To: FreeBSD-Hackers@freebsd.org User-Agent: KMail/1.9.7 MIME-Version: 1.0 Content-Disposition: inline Date: Mon, 22 Oct 2007 22:11:51 +0200 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <200710222211.51590.Danovitsch@vitsch.net> Cc: Subject: Floating point in interrupt handler X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 22 Oct 2007 20:30:34 -0000 Hi all, For a work related project I'm trying to create a sort of real-time control loop. I've written a driver for a PCI data acquisition card that features a number of digital-to-analog and analog-to-digital converters. The goal is to create a control loop that runs about 10000 times a second and can be altered on the fly. The control loop should always run, running the rest of FreeBSD (kernel and userland) is less important. To achieve this I've been playing with some modifications to hardclock(). I have added hooks to hardclock() that will call my external loop function once attached. The loop function lives in a seperate kernel module that can be loaded and unloaded on the fly. After changing HZ to 10000 I now have a function that gets called 10000 times a second that can manipulate the outputs of the acquisition board. As a first test I've created a simple loop that uses integer arithmetic and a lookup table to create a sine wave on one of the DAC outputs. This works like a charm, but I really would like to be able to use floating point instructions to create more advanced control loops. So I've added asm inline functions to use the FPU's fsin and fcos operands. At the start of my loop function I (try to) save the FPU state with the following code : td = curthread; npxgetregs(td, &fpu_state); At the end of the function I restore the FPU state with : npxsetregs(td, &fpu_state); In between I currently have : // create a 500Hz sine wave, modulate it with a 2Hz sine wave and // let it have a 10.0 Volt amplitude t += 0.0001; set_dac_voltage(card, 0, fp_sin(500 * 2 * M_PI * t) * fp_sin(2 * 2 * M_PI * t) * 10.0); As uggly as the code may seem, it works and the output of the acquisition board shows the expected signal on a scope. While the code seems to do what it should, the kernel floods the console with the following message : kernel trap 22 with interrupts disabled As I'm completely new to saving/restoring the FPU state and working with it from within the kernel (and especially from within an interrupt handler) I'm not sure if all the above is the correct way to do things, so please correct me if I'm doing things wrong. I'm also not sure if the FPU state is correctly restored and I haven't done any tests (yet) to check this, but judging by the amount of trap-22 messages I'm getting, I must be doing something wrong ;-) I have done some Googling and found a couple of interresting threads to read : http://unix.derkeiler.com/Mailing-Lists/FreeBSD/hackers/2007-03/msg00096.html http://lists.freebsd.org/pipermail/freebsd-emulation/2007-April/003448.html But what I haven't found is a description of exactly what the kernel is missing to allow floating point operations to be done there. Can anyone tell if I'm saving/restoring the FPU state the right way in the code snippets above and what I should do to correctly stop trap 22 from occuring? Thanks, -- Daan