/* Copyright 2017 Joseph Wasson * * 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, see . */ #include "process_steno.h" #include "quantum_keycodes.h" #include "eeprom.h" #include "keymap_steno.h" #include "virtser.h" #include // TxBolt Codes #define TXB_NUL 0 #define TXB_S_L 0b00000001 #define TXB_T_L 0b00000010 #define TXB_K_L 0b00000100 #define TXB_P_L 0b00001000 #define TXB_W_L 0b00010000 #define TXB_H_L 0b00100000 #define TXB_R_L 0b01000001 #define TXB_A_L 0b01000010 #define TXB_O_L 0b01000100 #define TXB_STR 0b01001000 #define TXB_E_R 0b01010000 #define TXB_U_R 0b01100000 #define TXB_F_R 0b10000001 #define TXB_R_R 0b10000010 #define TXB_P_R 0b10000100 #define TXB_B_R 0b10001000 #define TXB_L_R 0b10010000 #define TXB_G_R 0b10100000 #define TXB_T_R 0b11000001 #define TXB_S_R 0b11000010 #define TXB_D_R 0b11000100 #define TXB_Z_R 0b11001000 #define TXB_NUM 0b11010000 #define TXB_GRP0 0b00000000 #define TXB_GRP1 0b01000000 #define TXB_GRP2 0b10000000 #define TXB_GRP3 0b11000000 #define TXB_GRPMASK 0b11000000 #define TXB_GET_GROUP(code) ((code & TXB_GRPMASK) >> 6) #define BOLT_STATE_SIZE 4 #define GEMINI_STATE_SIZE 6 #define MAX_STATE_SIZE GEMINI_STATE_SIZE static uint8_t state[MAX_STATE_SIZE] = {0}; static uint8_t chord[MAX_STATE_SIZE] = {0}; static int8_t pressed = 0; static steno_mode_t mode; static const uint8_t boltmap[64] PROGMEM = {TXB_NUL, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_S_L, TXB_S_L, TXB_T_L, TXB_K_L, TXB_P_L, TXB_W_L, TXB_H_L, TXB_R_L, TXB_A_L, TXB_O_L, TXB_STR, TXB_STR, TXB_NUL, TXB_NUL, TXB_NUL, TXB_STR, TXB_STR, TXB_E_R, TXB_U_R, TXB_F_R, TXB_R_R, TXB_P_R, TXB_B_R, TXB_L_R, TXB_G_R, TXB_T_R, TXB_S_R, TXB_D_R, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_NUM, TXB_Z_R}; static void steno_clear_state(void) { memset(state, 0, sizeof(state)); memset(chord, 0, sizeof(chord)); } static void send_steno_state(uint8_t size, bool send_empty) { for (uint8_t i = 0; i < size; ++i) { if (chord[i] || send_empty) { #ifdef VIRTSER_ENABLE virtser_send(chord[i]); #endif } } } void steno_init() { if (!eeconfig_is_enabled()) { eeconfig_init(); } mode = eeprom_read_byte(EECONFIG_STENOMODE); } void steno_set_mode(steno_mode_t new_mode) { steno_clear_state(); mode = new_mode; eeprom_update_byte(EECONFIG_STENOMODE, mode); } /* override to intercept chords right before they get sent. * return zero to suppress normal sending behavior. */ __attribute__((weak)) bool send_steno_chord_user(steno_mode_t mode, uint8_t chord[6]) { return true; } __attribute__((weak)) bool postprocess_steno_user(uint16_t keycode, keyrecord_t *record, steno_mode_t mode, uint8_t chord[6], int8_t pressed) { return true; } __attribute__((weak)) bool process_steno_user(uint16_t keycode, keyrecord_t *record) { return true; } static void send_steno_chord(void) { if (send_steno_chord_user(mode, chord)) { switch (mode) { case STENO_MODE_BOLT: send_steno_state(BOLT_STATE_SIZE, false); #ifdef VIRTSER_ENABLE virtser_send(0); // terminating byte #endif break; case STENO_MODE_GEMINI: chord[0] |= 0x80; // Indicate start of packet send_steno_state(GEMINI_STATE_SIZE, true); break; } } steno_clear_state(); } uint8_t *steno_get_state(void) { return &state[0]; } uint8_t *steno_get_chord(void) { return &chord[0]; } static bool update_state_bolt(uint8_t key, bool press) { uint8_t boltcode = pgm_read_byte(boltmap + key); if (press) { state[TXB_GET_GROUP(boltcode)] |= boltcode; chord[TXB_GET_GROUP(boltcode)] |= boltcode; } else { state[TXB_GET_GROUP(boltcode)] &= ~boltcode; } return false; } static bool update_state_gemini(uint8_t key, bool press) { int idx = key / 7; uint8_t bit = 1 << (6 - (key % 7)); if (press) { state[idx] |= bit; chord[idx] |= bit; } else { state[idx] &= ~bit; } return false; } bool process_steno(uint16_t keycode, keyrecord_t *record) { switch (keycode) { case QK_STENO_BOLT: if (!process_steno_user(keycode, record)) { return false; } if (IS_PRESSED(record->event)) { steno_set_mode(STENO_MODE_BOLT); } return false; case QK_STENO_GEMINI: if (!process_steno_user(keycode, record)) { return false; } if (IS_PRESSED(record->event)) { steno_set_mode(STENO_MODE_GEMINI); } return false; case STN__MIN ... STN__MAX: if (!process_steno_user(keycode, record)) { return false; } switch (mode) { case STENO_MODE_BOLT: update_state_bolt(keycode - QK_STENO, IS_PRESSED(record->event)); break; case STENO_MODE_GEMINI: update_state_gemini(keycode - QK_STENO, IS_PRESSED(record->event)); break; } // allow postprocessing hooks if (postprocess_steno_user(keycode, record, mode, chord, pressed)) { if (IS_PRESSED(record->event)) { ++pressed; } else { --pressed; if (pressed <= 0) { pressed = 0; send_steno_chord(); } } } return false; } return true; } ='n92' href='#n92'>92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
/****************************************************************************
 * (C) 2002-2003 - Rolf Neugebauer - Intel Research Cambridge
 * (C) 2002-2003 University of Cambridge
 * (C) 2004      - Mark Williamson - Intel Research Cambridge
 ****************************************************************************
 *
 *        File: common/schedule.c
 *      Author: Rolf Neugebauer & Keir Fraser
 *              Updated for generic API by Mark Williamson
 *
 * Description: CPU scheduling
 *              implements A Borrowed Virtual Time scheduler.
 *              (see Duda & Cheriton SOSP'99)
 */

#include <xen/config.h>
#include <xen/init.h>
#include <xen/lib.h>
#include <xen/sched.h>
#include <xen/delay.h>
#include <xen/event.h>
#include <xen/time.h>
#include <xen/timer.h>
#include <xen/perfc.h>
#include <xen/sched-if.h>
#include <xen/softirq.h>

/* all per-domain BVT-specific scheduling info is stored here */
struct bvt_vcpu_info
{
    struct list_head    run_list;         /* runqueue list pointers */
    u32                 avt;              /* actual virtual time */
    u32                 evt;              /* effective virtual time */
    int                 migrated;         /* migrated to a new CPU */
    struct vcpu         *vcpu;
    struct bvt_dom_info *inf;
};

struct bvt_dom_info
{
    struct domain       *domain;          /* domain this info belongs to */
    u32                 mcu_advance;      /* inverse of weight */
    int                 warpback;         /* warp?  */
    int                 warp;             /* warp set and within the warp 
                                             limits*/
    s32                 warp_value;       /* virtual time warp */
    s_time_t            warpl;            /* warp limit */
    struct timer        warp_timer;       /* deals with warpl */
    s_time_t            warpu;            /* unwarp time requirement */
    struct timer        unwarp_timer;     /* deals with warpu */

    struct bvt_vcpu_info vcpu_inf[MAX_VIRT_CPUS];
};

struct bvt_cpu_info
{
    struct list_head    runqueue;
    unsigned long       svt;
};

#define BVT_INFO(p)   ((struct bvt_dom_info *)(p)->sched_priv)
#define EBVT_INFO(p)  ((struct bvt_vcpu_info *)(p)->sched_priv)
#define CPU_INFO(cpu) ((struct bvt_cpu_info *)(schedule_data[cpu]).sched_priv)
#define RUNLIST(p)    ((struct list_head *)&(EBVT_INFO(p)->run_list))
#define RUNQUEUE(cpu) ((struct list_head *)&(CPU_INFO(cpu)->runqueue))
#define CPU_SVT(cpu)  (CPU_INFO(cpu)->svt)

#define MCU            (s32)MICROSECS(100)    /* Minimum unit */
#define MCU_ADVANCE    10                     /* default weight */
#define TIME_SLOP      (s32)MICROSECS(50)     /* allow time to slip a bit */
#define CTX_MIN        (s32)MICROSECS(10)     /* Low limit for ctx_allow */
static s32 ctx_allow = (s32)MILLISECS(5);     /* context switch allowance */

static inline void __add_to_runqueue_head(struct vcpu *d)
{
    list_add(RUNLIST(d), RUNQUEUE(d->processor));
}

static inline void __add_to_runqueue_tail(struct vcpu *d)
{
    list_add_tail(RUNLIST(d), RUNQUEUE(d->processor));
}

static inline void __del_from_runqueue(struct vcpu *d)
{
    struct list_head *runlist = RUNLIST(d);
    list_del(runlist);
    runlist->next = NULL;
}

static inline int __task_on_runqueue(struct vcpu *d)
{
    return (RUNLIST(d))->next != NULL;
}


/* Warp/unwarp timer functions */
static void warp_timer_fn(void *data)
{
    struct bvt_dom_info *inf = data;
    struct vcpu *v = inf->domain->vcpu[0];

    vcpu_schedule_lock_irq(v);

    inf->warp = 0;

    /* unwarp equal to zero => stop warping */
    if ( inf->warpu == 0 )
    {
        inf->warpback = 0;
        cpu_raise_softirq(v->processor, SCHEDULE_SOFTIRQ);   
    }
    
    set_timer(&inf->unwarp_timer, NOW() + inf->warpu);

    vcpu_schedule_unlock_irq(v);
}

static void unwarp_timer_fn(void *data)
{
    struct bvt_dom_info *inf = data;
    struct vcpu *v = inf->domain->vcpu[0];

    vcpu_schedule_lock_irq(v);

    if ( inf->warpback )
    {
        inf->warp = 1;
        cpu_raise_softirq(v->processor, SCHEDULE_SOFTIRQ);   
    }
     
    vcpu_schedule_unlock_irq(v);
}

static inline u32 calc_avt(struct vcpu *v, s_time_t now)
{
    u32 ranfor, mcus;
    struct bvt_dom_info *inf = BVT_INFO(v->domain);
    struct bvt_vcpu_info *einf = EBVT_INFO(v);
    
    ranfor = (u32)(now - v->runstate.state_entry_time);
    mcus = (ranfor + MCU - 1)/MCU;

    return einf->avt + mcus * inf->mcu_advance;
}

/*
 * Calculate the effective virtual time for a domain. Take into account 
 * warping limits
 */
static inline u32 calc_evt(struct vcpu *d, u32 avt)
{
    struct bvt_dom_info *inf = BVT_INFO(d->domain);
    /* TODO The warp routines need to be rewritten GM */
 
    if ( inf->warp ) 
        return avt - inf->warp_value;
    else 
        return avt;
}

/**
 * bvt_init_vcpu - allocate BVT private structures for a VCPU.
 * Returns non-zero on failure.
 */
static int bvt_init_vcpu(struct vcpu *v)
{
    struct domain *d = v->domain;
    struct bvt_dom_info *inf;
    struct bvt_vcpu_info *einf;

    if ( (d->sched_priv == NULL) )
    {
        if ( (d->sched_priv = xmalloc(struct bvt_dom_info)) == NULL )
            return -1;
        memset(d->sched_priv, 0, sizeof(struct bvt_dom_info));
    }

    inf = BVT_INFO(d);

    v->sched_priv = &inf->vcpu_inf[v->vcpu_id];

    inf->vcpu_inf[v->vcpu_id].inf  = BVT_INFO(d);
    inf->vcpu_inf[v->vcpu_id].vcpu = v;

    if ( v->vcpu_id == 0 )
    {
        inf->mcu_advance = MCU_ADVANCE;
        inf->domain      = v->domain;
        inf->warpback    = 0;
        /* Set some default values here. */
        inf->warp        = 0;
        inf->warp_value  = 0;
        inf->warpl       = MILLISECS(2000);
        inf->warpu       = MILLISECS(1000);
        /* Initialise the warp timers. */
        init_timer(&inf->warp_timer, warp_timer_fn, inf, v->processor);
        init_timer(&inf->unwarp_timer, unwarp_timer_fn, inf, v->processor);
    }

    einf = EBVT_INFO(v);

    /* Allocate per-CPU context if this is the first domain to be added. */
    if ( CPU_INFO(v->processor) == NULL )
    {
        schedule_data[v->processor].sched_priv = xmalloc(struct bvt_cpu_info);
        BUG_ON(CPU_INFO(v->processor) == NULL);
        INIT_LIST_HEAD(RUNQUEUE(v->processor));
        CPU_SVT(v->processor) = 0;
    }

    if ( is_idle_vcpu(v) )
    {
        einf->avt = einf->evt = ~0U;
        BUG_ON(__task_on_runqueue(v));
        __add_to_runqueue_head(v);
    }
    else 
    {
        /* Set avt and evt to system virtual time. */
        einf->avt = CPU_SVT(v->processor);
        einf->evt = CPU_SVT(v->processor);
    }

    return 0;
}

static void bvt_wake(struct vcpu *v)
{
    struct bvt_vcpu_info *einf = EBVT_INFO(v);
    struct vcpu  *curr;
    s_time_t            now, r_time;