text: /*
   * MIT License (MIT)
   *
   * Copyright (C) 2017 Dmytro Kalchenko <dmytro.v.kalchenko@gmail.com>
   *
   * Permission is hereby granted, free of charge, to any person obtaining a
   * copy of this software and associated documentation files (the "Software"),
   * to deal in the Software without restriction, including without limitation
   * the rights to use, copy, modify, merge, publish, distribute, sublicense,
   * and/or sell copies of the Software, and to permit persons to whom the
   * Software is furnished to do so, subject to the following conditions:
   *
   * The above copyright notice and this permission notice shall be included in
   * all copies or substantial portions of the Software.
   *
   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
   * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   * DEALINGS IN THE SOFTWARE.
   */
  
  #include <arch/i386/cpu/gdt.h>
  
  
  uint64_t gdt_create_descriptor(uint32_t base, uint32_t limit, uint16_t flag)
  {
      uint64_t descriptor;
  
      // Create the high 32 bit segment
      descriptor  =  limit       & 0x000F0000;         // set limit bits 19:16
      descriptor |= (flag <<  8) & 0x00F0FF00;         // set type, p, dpl, s, g, d/b, l and avl fields
      descriptor |= (base >> 16) & 0x000000FF;         // set base bits 23:16
      descriptor |=  base        & 0xFF000000;         // set base bits 31:24
  
      // Shift by 32 to allow for low part of segment
      descriptor <<= 32;
  
      // Create the low 32 bit segment
      descriptor |= base  << 16;                       // set base bits 15:0
      descriptor |= limit  & 0x0000FFFF;               // set limit bits 15:0
  
  	return descriptor;
  }
  
  
  