0

I need to implement this function as a systemcall:

asmlinkage ​ long​​ sys_sqrt (​ float​ x);

Where the function gonna print the square root of n to the kernel log. I'm using kernel version 4.13 on 64bit virtual box.

I'm trying to implement the sqrt by using this technique

#include <linux/kernel.h>
#define SQRT_MAGIC_F 0x5f3759df 
 asmlinkage ​long​​ sys_sqrt(​float​ x);
{
  const float xhalf = 0.5f*x;

  union // get bits for floating value
  {
    float x;
    int i;
  } u;
  u.x = x;
  u.i = SQRT_MAGIC_F - (u.i >> 1);
  printk ("%f", (x*u.x*(1.5f - xhalf*u.x*u.x));
  return 0;
} 

This leads the compiler telling me "error: SSE register return with SSE disabled" on "printk ("%f", (x * u.x * (1.5f - xhalf * u.x * u.x));"

Another workaround I tried is separating the integer and the decimals like so

float ans = (x*u.x*(1.5f - xhalf*u.x*u.x);
int head = ans;
float tail_float = ans - head;
int tail = tail_float*10000;
printk ("%d.%03d", head,tail);

This leads the compiler telling me "error:SSE register return with SSE disabled" on "float ans = (x *u.x *(1.5f - xhalf * u.x* u.x);"

another thing i've tried is adding a kernel_fpu_begin & end between the function body but this leads "error: implicit declaration of function "kernel_fpu_begin"; did you mean "kernel_old_dev_t"

Any solution? Thank you so much.

Joko
  • 1
  • 1
  • How do you build your code? Is it put into a separate module? Do you use any non-standard compiler flags nor part of the kernel building process? – Some programmer dude Feb 17 '18 at 08:39
  • I'm compiling the kernel, so my command was "sudo make -j 4 && sudo make modules_install install -j 4" on /usr/src/linux-4.13 – Joko Feb 17 '18 at 08:42
  • 3
    Possible duplicate of [SSE register return with SSE disabled](https://stackoverflow.com/questions/1556142/sse-register-return-with-sse-disabled) – Tsyvarev Feb 17 '18 at 09:03
  • Is it your homework? Never use `float` in kernel... – Sam Protsenko Feb 17 '18 at 09:23
  • Yes, it is my home work. I've read about that in the last 2 day, but I must perform that float operation in kernel. – Joko Feb 17 '18 at 12:18
  • https://stackoverflow.com/questions/13886338/use-of-floating-point-in-the-linux-kernel – tehp Feb 17 '18 at 19:42

0 Answers0