Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[bitops] Add generic atomic bit test, set, and clear functions
Signed-off-by: Michael Brown <mcb30@ipxe.org>
- Loading branch information
Showing
4 changed files
with
216 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
#ifndef _BITS_BITOPS_H | ||
#define _BITS_BITOPS_H | ||
|
||
/** @file | ||
* | ||
* x86 bit operations | ||
* | ||
* We perform atomic bit set and bit clear operations using "lock bts" | ||
* and "lock btr". We use the output constraint to inform the | ||
* compiler that any memory from the start of the bit field up to and | ||
* including the byte containing the bit may be modified. (This is | ||
* overkill but shouldn't matter in practice since we're unlikely to | ||
* subsequently read other bits from the same bit field.) | ||
*/ | ||
|
||
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); | ||
|
||
#include <stdint.h> | ||
|
||
/** | ||
* Set bit atomically | ||
* | ||
* @v bit Bit to set | ||
* @v bits Bit field | ||
*/ | ||
static inline __attribute__ (( always_inline )) void | ||
set_bit ( unsigned int bit, volatile void *bits ) { | ||
volatile struct { | ||
uint8_t byte[ ( bit / 8 ) + 1 ]; | ||
} *bytes = bits; | ||
|
||
__asm__ __volatile__ ( "lock bts %1, %0" | ||
: "+m" ( *bytes ) : "Ir" ( bit ) ); | ||
} | ||
|
||
/** | ||
* Clear bit atomically | ||
* | ||
* @v bit Bit to set | ||
* @v bits Bit field | ||
*/ | ||
static inline __attribute__ (( always_inline )) void | ||
clear_bit ( unsigned int bit, volatile void *bits ) { | ||
volatile struct { | ||
uint8_t byte[ ( bit / 8 ) + 1 ]; | ||
} *bytes = bits; | ||
|
||
__asm__ __volatile__ ( "lock btr %1, %0" | ||
: "+m" ( *bytes ) : "Ir" ( bit ) ); | ||
} | ||
|
||
/** | ||
* Test and set bit atomically | ||
* | ||
* @v bit Bit to set | ||
* @v bits Bit field | ||
* @ret old Old value of bit (zero or non-zero) | ||
*/ | ||
static inline __attribute__ (( always_inline )) int | ||
test_and_set_bit ( unsigned int bit, volatile void *bits ) { | ||
volatile struct { | ||
uint8_t byte[ ( bit / 8 ) + 1 ]; | ||
} *bytes = bits; | ||
int old; | ||
|
||
__asm__ __volatile__ ( "lock bts %2, %0\n\t" | ||
"sbb %1, %1\n\t" | ||
: "+m" ( *bytes ), "=r" ( old ) | ||
: "Ir" ( bit ) ); | ||
return old; | ||
} | ||
|
||
/** | ||
* Test and clear bit atomically | ||
* | ||
* @v bit Bit to set | ||
* @v bits Bit field | ||
* @ret old Old value of bit (zero or non-zero) | ||
*/ | ||
static inline __attribute__ (( always_inline )) int | ||
test_and_clear_bit ( unsigned int bit, volatile void *bits ) { | ||
volatile struct { | ||
uint8_t byte[ ( bit / 8 ) + 1 ]; | ||
} *bytes = bits; | ||
int old; | ||
|
||
__asm__ __volatile__ ( "lock btr %2, %0\n\t" | ||
"sbb %1, %1\n\t" | ||
: "+m" ( *bytes ), "=r" ( old ) | ||
: "Ir" ( bit ) ); | ||
return old; | ||
} | ||
|
||
#endif /* _BITS_BITOPS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#ifndef _IPXE_BITOPS_H | ||
#define _IPXE_BITOPS_H | ||
|
||
/** @file | ||
* | ||
* Bit operations | ||
* | ||
*/ | ||
|
||
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); | ||
|
||
#include <bits/bitops.h> | ||
|
||
void set_bit ( unsigned int bit, volatile void *bits ); | ||
void clear_bit ( unsigned int bit, volatile void *bits ); | ||
int test_and_set_bit ( unsigned int bit, volatile void *bits ); | ||
int test_and_clear_bit ( unsigned int bit, volatile void *bits ); | ||
|
||
#endif /* _IPXE_BITOPS_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (C) 2016 Michael Brown <mbrown@fensystems.co.uk>. | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU General Public License as | ||
* published by the Free Software Foundation; either version 2 of the | ||
* License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, but | ||
* WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program; if not, write to the Free Software | ||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | ||
* 02110-1301, USA. | ||
* | ||
* You can also choose to distribute this program under the terms of | ||
* the Unmodified Binary Distribution Licence (as given in the file | ||
* COPYING.UBDL), provided that you have satisfied its requirements. | ||
*/ | ||
|
||
FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL ); | ||
|
||
/** @file | ||
* | ||
* Bit operations self-tests | ||
* | ||
*/ | ||
|
||
/* Forcibly enable assertions */ | ||
#undef NDEBUG | ||
|
||
#include <stdint.h> | ||
#include <string.h> | ||
#include <assert.h> | ||
#include <ipxe/bitops.h> | ||
#include <ipxe/test.h> | ||
|
||
/** | ||
* Perform bit operations self-tests | ||
* | ||
*/ | ||
static void bitops_test_exec ( void ) { | ||
uint8_t bits[32]; | ||
|
||
/* Initialise bits */ | ||
memset ( bits, 0, sizeof ( bits ) ); | ||
|
||
/* Test set_bit() */ | ||
set_bit ( 0, bits ); | ||
ok ( bits[0] == 0x01 ); | ||
set_bit ( 17, bits ); | ||
ok ( bits[2] == 0x02 ); | ||
set_bit ( 22, bits ); | ||
ok ( bits[2] == 0x42 ); | ||
set_bit ( 22, bits ); | ||
ok ( bits[2] == 0x42 ); | ||
|
||
/* Test clear_bit() */ | ||
clear_bit ( 0, bits ); | ||
ok ( bits[5] == 0x00 ); | ||
bits[5] = 0xff; | ||
clear_bit ( 42, bits ); | ||
ok ( bits[5] == 0xfb ); | ||
clear_bit ( 42, bits ); | ||
ok ( bits[5] == 0xfb ); | ||
clear_bit ( 44, bits ); | ||
ok ( bits[5] == 0xeb ); | ||
|
||
/* Test test_and_set_bit() */ | ||
ok ( test_and_set_bit ( 0, bits ) == 0 ); | ||
ok ( bits[0] == 0x01 ); | ||
ok ( test_and_set_bit ( 0, bits ) != 0 ); | ||
ok ( bits[0] == 0x01 ); | ||
ok ( test_and_set_bit ( 69, bits ) == 0 ); | ||
ok ( bits[8] == 0x20 ); | ||
ok ( test_and_set_bit ( 69, bits ) != 0 ); | ||
ok ( bits[8] == 0x20 ); | ||
ok ( test_and_set_bit ( 69, bits ) != 0 ); | ||
ok ( bits[8] == 0x20 ); | ||
|
||
/* Test test_and_clear_bit() */ | ||
ok ( test_and_clear_bit ( 0, bits ) != 0 ); | ||
ok ( bits[0] == 0x00 ); | ||
ok ( test_and_clear_bit ( 0, bits ) == 0 ); | ||
ok ( bits[0] == 0x00 ); | ||
bits[31] = 0xeb; | ||
ok ( test_and_clear_bit ( 255, bits ) != 0 ); | ||
ok ( bits[31] == 0x6b ); | ||
ok ( test_and_clear_bit ( 255, bits ) == 0 ); | ||
ok ( bits[31] == 0x6b ); | ||
ok ( test_and_clear_bit ( 255, bits ) == 0 ); | ||
ok ( bits[31] == 0x6b ); | ||
} | ||
|
||
/** Bit operations self-test */ | ||
struct self_test bitops_test __self_test = { | ||
.name = "bitops", | ||
.exec = bitops_test_exec, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters