SPB Git

Repository Public

dit.c 3.1 KB 94 lines c

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : QYVORA8 * File             : Sources/QyvoraCSupport/dit.c9 * Module           : Qyvora CSupport10 * Repository       : https://git.spboucher.ai/qyvora11 * Project Website  : Not specified12 * Created          : 2026-12-0413 * Last Modified    : 2026-12-0414 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: contact@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243#include "qycsupport.h"4445#include <stdatomic.h>46#include <stddef.h>47#include <sys/sysctl.h>4849#if defined(__aarch64__)5051/* -1 unknown, 0 absent, 1 present. Atomic: two first calls may both query the system and store the same value. */52static _Atomic int qy_dit_capability = -1;5354int qy_dit_supported(void) {55    int known = atomic_load_explicit(&qy_dit_capability, memory_order_relaxed);56    if (known < 0) {57        int value = 0;58        size_t size = sizeof value;59        known = (sysctlbyname("hw.optional.arm.FEAT_DIT", &value, &size, NULL, 0) == 0 && value != 0) ? 1 : 0;60        atomic_store_explicit(&qy_dit_capability, known, memory_order_relaxed);61    }62    return known;63}6465/* DIT is bit 24 of the system register S3_3_C4_C2_5; it is readable and writable at EL0. */66static inline int qy_dit_read(void) {67    unsigned long value = 0;68    __asm__ volatile("mrs %0, s3_3_c4_c2_5" : "=r"(value));69    return (int)((value >> 24) & 1);70}7172int qy_dit_enabled(void) {73    return qy_dit_supported() ? qy_dit_read() : 0;74}7576int qy_dit_enable(void) {77    if (!qy_dit_supported() || qy_dit_read()) return 1;78    __asm__ volatile(".inst 0xd503415f"); /* msr dit, #1 */79    return 0;80}8182void qy_dit_restore(int previous) {83    if (!previous) __asm__ volatile(".inst 0xd503405f"); /* msr dit, #0 */84}8586#else8788int qy_dit_supported(void) { return 0; }89int qy_dit_enabled(void) { return 0; }90int qy_dit_enable(void) { return 1; }91void qy_dit_restore(int previous) { (void)previous; }9293#endif94
Files main
Menu

Theme