From owner-freebsd-mips@FreeBSD.ORG Mon Jun 3 14:04:24 2013 Return-Path: Delivered-To: freebsd-mips@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by hub.freebsd.org (Postfix) with ESMTP id 10259199; Mon, 3 Jun 2013 14:04:24 +0000 (UTC) (envelope-from edschouten@gmail.com) Received: from mail-ve0-x230.google.com (mail-ve0-x230.google.com [IPv6:2607:f8b0:400c:c01::230]) by mx1.freebsd.org (Postfix) with ESMTP id BA5861ED6; Mon, 3 Jun 2013 14:04:23 +0000 (UTC) Received: by mail-ve0-f176.google.com with SMTP id c13so2783512vea.7 for ; Mon, 03 Jun 2013 07:04:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:date:x-google-sender-auth:message-id:subject :from:to:cc:content-type; bh=5mOuUcOvJhlE7fzQiapnilkp6TFn7nTM41OAxUuOws8=; b=S4u1MiTYxZLIYpMI9pEq+Jgnv0tsImRWnKaIG7avhfuJiYtqN434gO4X4HJ737gdlE nYSAMAXZa28xvfZhYPwssZoOxY8xnQaurLMJ5eEteOXGQKZdj0pOs00Vu6b4n5LFzRYw fNY7ww7/hXYiIXD8OA8T62vMQ9acNHZ+EkxbPg+b4Nz8pjVX16FARtb/glX4rPMClYu4 m7bZFQX8UhcshntZ+L9N/9cuAA4bbTTSOztLacr+COUDC3oJ8LMm5FAEUZ/PU2DKiH+F 0nTmgR2pgUu8ZjeoyHZCWEzBz/ukncgtLMIkbbrsb8zDIThl0k3UKJ6BvV/OzR9MZxPL ddeA== MIME-Version: 1.0 X-Received: by 10.52.183.170 with SMTP id en10mr14491274vdc.5.1370268263228; Mon, 03 Jun 2013 07:04:23 -0700 (PDT) Sender: edschouten@gmail.com Received: by 10.220.107.139 with HTTP; Mon, 3 Jun 2013 07:04:23 -0700 (PDT) Date: Mon, 3 Jun 2013 16:04:23 +0200 X-Google-Sender-Auth: Inb7_DbHLzfo8UoJdPCaDKalzAk Message-ID: Subject: Kernelspace C11 atomics for MIPS From: Ed Schouten To: freebsd-mips@freebsd.org Content-Type: text/plain; charset=UTF-8 Cc: freebsd-arch@freebsd.org X-BeenThere: freebsd-mips@freebsd.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Porting FreeBSD to MIPS List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 03 Jun 2013 14:04:24 -0000 Hi, As of r251230, it should be possible to use C11 atomics in kernelspace, by including ! Even when not using Clang (but GCC 4.2), it is possible to use quite a large portion of the API. A couple of limitations: - The memory order argument is simply ignored, making all the calls do a full memory barrier. - At least Clang allows you to do arithmetic on C11 atomics directly (e.g. "a += 5" == "atomic_fetch_add(&a, 5)"), which is of course not possible to mimick. - The atomic functions only work on 1,2,4,8-byte types, which is probably a good thing. Amazingly, it turns out that it most of the architectures, with the exception of ARM and MIPS. To make MIPS work, we need to implement some of the __sync_* functions that are described here: http://gcc.gnu.org/onlinedocs/gcc-4.1.2/gcc/Atomic-Builtins.html Some time ago I already added some of these functions to our libcompiler-rt in userspace, to make atomics work there. Unfortunately, these functions were quite horribly implemented, as I tried to build them on top of , which is far from trivial/efficient. It is also restricted to 4 and 8-byte types. That's why I thought: why not spend some time learning MIPS assembly and write some decent implementations for these functions? The result: http://80386.nl/pub/mips-stdatomic.txt For now, please focus on sys/mips/mips/stdatomic.c. It implements all the __sync_* functions called by for 1, 2, 4 and 8 byte types. There is some testing code in there as well, which can be ignored. This code disassembles to the following: http://80386.nl/pub/mips-stdatomic-disasm.txt As I don't own a MIPS system myself, I was thinking about tinkering a bit with qemu to see whether these functions work properly. My questions are: - Does anyone have any comments on the C code and/or the machine code generated? Are there some nifty tricks I can apply to make the machine code more efficient that I am unaware o? - Is there anyone interested in testing this code a bit more thoroughly on physical hardware? - Would anyone mind if I committed this to HEAD? Thanks, -- Ed Schouten